[PyQt] the mechanism behind qt signal and slot

Sathishkumar Duraisamy flowerslab at gmail.com
Tue Sep 21 05:39:20 BST 2010


hi

The following is an extract from  the book "Foundations of Qt Development"
by Johan Thelin. This explains how signal is implemented in Qt. Thanks
Mr.Johan Thelin.




"Signals and slots are implemented by Qt using function pointers. When
calling emit with the signal as argument, you actually call the signal. The
signal is a function implemented in the source file generated by the moc.
This function calls any slots connected to the signal using the meta-objects
of the objects holding the connected slots.

The meta-objects contain function pointers to the slots, along with their
names and argument types.They also contain a list of the available Signals
and their names and argument types. When calling connect,
you ask the meta-object to add the slot to the signal’s calling list. If the
arguments match, the connection is made.When matching arguments, the match
is checked only for the arguments accepted by the slot. This
means that a slot that does not take any arguments matches all signals. The
arguments not accepted by the slot are simply dropped by the signal-emitting
code."


On Mon, Sep 20, 2010 at 8:57 AM, Von <vontio at gmail.com> wrote:

> Many thanks,Andreas.
>
>
> On Mon, Sep 20, 2010 at 2:09 AM, Andreas Pakulat <apaku at gmx.de> wrote:
>
>> On 19.09.10 18:15:15, Von wrote:
>> > Hi,Algis,many thanks for all these informations.
>> > At first I thought QtCore.QtObject.connect as static method of
>> > QtObject,later I realized that,this is py style of calling super method.
>> > That's why I asked question (3).
>> > The document of pyqt says "The code (or component) that emits the signal
>> > does not know or care if the signal is being used."
>> > In my head,the implementation of signal and slot is an observer pattern.
>> > Pseudo-code:
>> > def connect(source,signal,slot):
>> >     if(not source.slots[signal]):
>> >         source.slots[signal] = []
>> >     source.slots[signal].append(slot)
>> >
>> > def emit(signal,param):
>> >     for slot in self.slots[signal]:
>> >         slot(param)
>> > I am wondering why signal does not know what slots bind with it?
>>
>> That pseudo code is pretty close to reality, except that (in C++ Qt)
>> emit is just a macro and the for slot in slots-stuff is in the
>> moc-generated code. So the signal does know which slots are connected,
>> but whoever call emit mysignal doesn't know and doesn't care. Thats the
>> point of loosely coupled objects and encapsulation. The code that emits
>> the signal always does so, no matter wether there are any connected
>> slots or not. So the for-loop might not run its body even once, but the
>> 'emission' is always done. Thats what is meant with 'the code that emits
>> the signal doesn't know or care if the signal is being used'
>>
>> Andreas
>>
>> --
>> Your step will soil many countries.
>> _______________________________________________
>> PyQt mailing list    PyQt at riverbankcomputing.com
>> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
>>
>
>
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20100921/44311fb0/attachment-0001.html>


More information about the PyQt mailing list