[PyQt] the mechanism behind qt signal and slot

Andreas Pakulat apaku at gmx.de
Sun Sep 19 19:09:20 BST 2010


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.


More information about the PyQt mailing list