[Kde-bindings] Hacking moc-generated code

Richard Dale Richard_Dale at tipitina.demon.co.uk
Fri Sep 9 11:11:18 UTC 2005


On Wednesday 31 August 2005 06:33, Eric Jardim wrote:
> 2005/8/31, Marcus <mathpup at mylinuxisp.com>:
> > I would like to hear more about this because getting signals and slots
> > working
> > seems to be one of the hardest parts. I understand that Qt 4.x makes some
> > changes, but I also gather that there is still a great deal of voodoo in
> > the
> > moc-generated code.
>
> Well, actually, signals and slots are working normally in python-qt4. I
> used a simple approach. I created dispatcher classes for each type of slot.
> It is impossible to connect a signal to a Python function. There must be a
> C++ slot. So I have a dispatcher class for every possible signature ex:
> PythonSlot_int_int -> void slot(int, int)
> and so on. Of course I have a script that write this code for me. You just
> have to say (textualy) which signatures you want to generate.
>
> It is the only way I see to make it works. The limitation of this approach
> are new types of slot signatures. Well, we can solve this creating a
> central registry for slot dispachers. Hey, remember now... I've made this
> :) But needs more testing.
> Returning to MOC, I just need to play with it, only because if I want to
> run Python extended classes (of QObject/QWidget) on C++ (like embedded C++
> or using Python objects in QtDesigner). I am studing it, but I am not very
> sure that it will work.
>
> The problem is that the C++ metaobject is static. We need dynamic
> metaobjects! And that is the whole trick.
I wrote a blog about the format of the Qt4 QMetaObjects here:
http://www.kdedevelopers.org/node/1149

You can override QObject::metaObject() as it's a virtual method, and then 
construct a QMetaObject dynamically and return that instead of the usual 
statically allocated one. They are actually easier to construct dynamically 
than the Qt3 ones. It just comes down to constructing a string table full of 
null terminated strings, and a corresponding array of numeric pointers that 
give the offsets of the strings. This stuff can be derived from arrays of 
strings containing the slot and signal type signatures, that were used to 
declare signals and slots in the qtruby code. You also need to obtain the 
QMetaObject of the superclass, to construct a new one.

A slot is invoked via QObject::qt_metacall() which is also a virtual method 
that you can override. You need to marshall the arguments from C++ format 
your scripting language from an array of 'void *'s that qt_metacall() 
expects.

To emit a signal you need to call QObject->metaObject()->activate() with your 
scripting languages args converted to C++ ones in the same  'void*' array for 
qt_metacall().

-- Richard



More information about the Kde-bindings mailing list