libqtlua-list
[Top][All Lists]
Advanced

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

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


From: Alexandre Becoulet
Subject: Re: [Libqtlua-list] Accessing QObject inside a property in Lua
Date: Thu, 13 Jan 2011 01:07:08 +0100
User-agent: KMail/1.13.5 (Linux/2.6.36-ARCH; KDE/4.5.4; x86_64; ; )

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.

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.

> > Lua/Qt values convertion on property access and method call is performed
> > by raw_get_object and raw_set_object in src/qtluamember.cc. 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
http://www.nongnu.org/libqtlua/manual/node5.html
http://doc.qt.nokia.com/4.6/qmetatype.html#Type-enum

> 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]