[Kde-bindings] sinal slot handling in script language
Richard Dale
rdale at foton.es
Fri Oct 20 08:25:04 UTC 2006
On Friday 20 October 2006 05:38, Dongxu Ma wrote:
> Hi all,
>
> I am recently learning QT4 and related binding mechanism. This question
> troubled me very much,
> say, I declared a subclass and its signal/slots in binding language. How is
> the signal/slot handled then?
> Since in C++ moc will generate a dedicated file which contains some binary
> form information for this.
> This file is also linked during compiling.
> I also noticed the qt signal macro is substituted to 'signal:' , while this
> is not a c++ reserved word (anyone
> correct me since I am not a c++ expert). So how is such qt-spefic stuff
> handled?
>
> Can someone guide me any document?
The moc preprocessor generates code to create an instance of a QMetaObject for
classes that have the 'Q_OBJECT' macro in them. The QMetaObject is accessed
by the QObject::metaObject() virtual method. So when you connect a signal to
a slot the Qt runtime finds the QMetaObjects for the two instances to be
connected and finds the matching slots and signals in it.
In PerlQt, QtRuby and Qyoto the QMetaObject for a QObject subclass with slots
and/or signals is constructed at runtime. The QMetaObject is identical to the
one that the moc generates statically, and the Qt runtime can't tell the
difference. The QObject::metaObject() virtual method is overriden by the
bindings runtime, and when it is called a dynamically constructed QMetaObject
is returned.
When a slot or signal is invoked a virtual method called
QObject::qt_metacall() is called. It contains an array the holds the C++
argument values for the slot, and a numeric identifier that allows the slot
to be found via the QMetaObject. The language bindiings runtime must override
this method and detect whether a C++ slot is being called, or if the target
slot is one declared in the bindings language. If the target isn't C++, the
bindings runtime needs to marshall the args from C++ to the bindings language
and invoke the slot. After the slot has been called it needs to marshall any
reply value from the bindings language back to C++.
A third method is used by the Qt runtime to emit signals called
QMetaObject::activate(). It has an array of C++ values as an argument and so
you must marshall the values emitted by the signal in the bindings language
to C++ ones (that array is the one that the qt_metacall() method takes too).
As an example, you can read about the ruby bindings here:
http://developer.kde.org/language-bindings/ruby/index.html
The PyQt bindings take a different approach, and use a 'proxy instance' of a
QObject which you connect to, and it in turn forwards to slot invocations to
the actual python instance.
-- Richard
More information about the Kde-bindings
mailing list