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: Mon, 17 Jan 2011 01:21:28 +0100
User-agent: KMail/1.13.5 (Linux/2.6.36-ARCH; KDE/4.5.5; x86_64; ; )

On Friday, January 14, 2011 05:39:11 pm 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() ?

You are right it is, but we still have to find a way to cast MainWindow 
pointer to QObject pointer dynamically. I'll try to figure it out.

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

Right, C++ access will be tricky.

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

It should not take too long for me to implement, it's just a matter of 
accessing a QMap in the 'default:' section of a switch statement to handle 
dynamically registered types cases. I have precise idea of how user callbacks 
should be implemented and will update the manual with examples.

You may want to spend time on features you need for your applications, like 
custom delegates and other things we discussed instead of studying all behind 
the scene hacks of the Qt meta object system to implement a simple feature:).

-- 
Alexandre



reply via email to

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