[Top][All Lists]

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

Re: [Libqtlua-list] Making a QObject instanciatable from Lua

From: Alexandre Becoulet
Subject: Re: [Libqtlua-list] Making a QObject instanciatable from Lua
Date: Wed, 25 Jan 2012 23:21:33 +0100
User-agent: KMail/4.7.4 (Linux/3.1.5-1-ARCH; KDE/4.7.4; x86_64; ; )

On Tuesday, January 24, 2012 01:31:03 AM Edwin Marshall wrote:


> Anyways, I'm designing a program[1] that defines basic components, such as
> Action, Item, and Container. For the purpose of this email, I will focus on
> the Container component, which has the sole purpose of containing items and
> applying arbitrary restrictions to its contents. Specifically, given a
> Container named Hand, the following restrictions would apply:
>    - contains cards of any type (club, diamond, spade, heart)
>    - may contain no more than 5 cards
>    - Is only view-able by the player (hidden from opponent's view)
> What I'd like to do is define a generic class in C++ named Container[2]
> which could then be accessed and instantiated in lua as a table. It seems
> as though UserData or UserObject is what I need.

The proxy classes provided in QtLua allow to access Qt generic container 
classes from lua script. You may want to write such a class for your specific 
container class, this way you do not have to make your container class 
specific to lua or QtLua.

> However, In addition to
> not seeing any example code of how to use such a defined object without
> first instantiating it (eg, passing an instance to lua), I'd like to find a
> way to do so that doesn't tightly couple QtLua to the Container class,

An other approach which would not make your class inherit from UserData is to 
write a QObject based class and use the Qt slots and properties to control 
your object from lua.

> should I decide to revert to a different binding language (Such as
> QtScript). My first instinct is to employee multiple inheritance, but I
> can't help but think this is sloppy, and that there is a cleaner way to do
> this.
> In short, my question is this:
>    - How do I wrap a class and pass it to lua, rather than wrapping an
>    instance of that class to pass to lua?

You can not pass C++ classes to lua. You would need some kind of binding to 
achieve something similar to this and QtLua is not a binding. The closer 
approach is to take advantages of the Qt meta object system to make QtLua 
aware of some members of your C++ class when it deals with an object of that 
type. This mechanism is the same as the one used in QtScript. You may have to 
provide a QtLua::Function which constructs a new object of that class if it 
needs to be instantiated from lua script.



reply via email to

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