[Top][All Lists]

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

Re: [Libqtlua-list] Accessing QObject inside a property in Lua

From: Gustavo de Sá Carvalho Honorato
Subject: Re: [Libqtlua-list] Accessing QObject inside a property in Lua
Date: Fri, 14 Jan 2011 14:39:11 -0200

2011/1/12 Alexandre Becoulet <address@hidden>
> On Wednesday, January 12, 2011 02:03:08 am you wrote:
> Hi,
> > >> The problem occurs when I try to access the MainWindow by typing
> > >> app.mainWindow:close() in Lua Console, and I got the error "error:
> > >> [string ""]:1: attempt to index field 'mainWindow' (a nil value)".
> > >>
> > >> It's quite weird because when I type list(app) I can see that the
> > >> property mainWindow has a value QtLua::Property<MainWindow*>
> > >> mainWindow = Application::mainWindow.
> > >
> > > QtLua has no way to guess how to convert your declared meta type to a
> > > QtLua::Value. Try using a QObject* or QWidget* instead of a MainWindow*
> > > pointer type.
> >
> > Sorry, I gave you an incomplete information. MainWindow inherits from
> > QMainWindow which inherits from QWidget. I've registered MainWindow*
> > type because when I tried to access mainWindow property, I was getting
> > "QMetaProperty::read: Unable to handle unregistered datatype" error.
> I guessed that. The problem is once registered as a Qt meta type, MainWindow*
> and QObject* are just different meta types. Qt meta types system does not
> enable QtLua to guess at runtime that MainWindow* can be interpreted as a
> QObject*, due to C++ inheritance rules.

It's not possible to discover that MainWindow inherits from QObject by
searching its superclasses using the method
mainWindow->metaObject()->superClass() ?

> Qt meta types unknown to QtLua are converted to nil values.
> So you have to use QObject* or QWidget* type with Q_PROPERTY() property
> declaration instead of MainWindow* because these types are already known to Qt
> and have convertion code in QtLua. Value convertion will work and QtLua should
> still dynamically expose the object as a MainWindow with its specific members.

The problem with this approach is that in C++ side I'll have more
generic properties and if I need to access those properties in C++
code I'll need to use typecasting in every access to those properties.
I think a better approach is to add support to QtLua to handle custom
types. I can implement this feature if you are too busy, it should be
good for me to improve my knowledge of QtLua. If you agree, I'll just
need some directions because I don't know where to start.

> > > Lua/Qt values convertion on property access and method call is performed
> > > by raw_get_object and raw_set_object in src/ Supporting
> > > dynamic declaration of new types would require registering two callbacks
> > > to implement value convertion, I can implement this if there is a real
> > > need.
> >
> > It is necessary only for types which not inherits from QObject, am I
> > right?
> It would be necessary for all types which are not handled,
> even for pointer types which inherit from QObject.
> See
> > Implementing this will enable custom defined types with
> > QT_DECLARE_METATYPE to be handled in Lua code, is it?
> Yes. It would allow to provide callback to convert any Qt/C++ type to a lua
> value in both directions. For classes inheriting from QObject, QtLua could
> even provide template callback functions which just need to be registered with
> the class as template parameter. It would take place just after
> qRegisterMetaType invocation.
> --
> Alexandre

reply via email to

[Prev in Thread] Current Thread [Next in Thread]