lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Wed, 2011-04-13 at 11:06 +0200, Jerome Vuarand wrote:
> QAbstractItemModel is a special kind of beast, I've got trouble
> wrapping my head around it even in C++. Qt wants total control over
> the lifetime of items.
> 
> So the same rules as the C++ ones apply? If in C++ I can explicitly
> delete a QObject at any time (which is not the case for a
> QAbstractItemModel item, but it is for most widgets), I can let Lua
> collect it (which I agree is rarely what you want for widgets)?

Yes, the same rules apply. obj:delete() calls "delete p" on the boxed
pointer "p" in the userdata "obj" (with whatever C++ consequences) and
sets the boxed pointer to NULL. You should not use it afterwards (any
use of NULL boxed pointer uses lua_error).

o = QObject.new_local()

is equivalent to the following code:

o = QObject.new()
o.__gc = o.delete

All objects in lqt have a custom GC function that look for a __gc field
in the userdata environment, which is a table where __newindex stores
values.

So you can let Lua collect your values, but the default operation is to
do nothing. To actually delete the boxed pointer, set the __gc field to
"delete" or just create the object with new_local().