Connect and dtors

Hi,

I’d like to know whether signals and slot are “synchronous” or not. Let’s assume I have a bunch of drawn objects of class A, that share a pointer to some other objects of class B. I want to be able to change dynamically the A->B relationship, and in particular, to let all A object know when B are destroyed. I thought about emitting a “WillBeDestoyed()” signal in B dtor, and connect all my A to that signal.
Is it a viable solution ? Are all the signals dispatched at once ? Or do I run the risk to have some signals caught at a time, and the others at a later time ?

Sorry for the vague question, but I hope the intent is clearer than my sentences… :wink:

Regards,

Hi,

If you use:TQObject::Connect(“B”, “WillBeDestroyed()”, “A”, objA1, “DoWhatNeeded()”);for every object objA1, objA2… of class A, the slot DoWhatNeeded() will be called anytime the signal WillBeDestroyed() is emitted, e.g. an object of class B is destroyed.

For more details see also root.cern.ch/root/HowtoSignalSlot.html

Cheers, Ilka

[quote=“aphecetche”]Hi,

I’d like to know whether signals and slot are “synchronous” or not. Let’s assume I have a bunch of drawn objects of class A, that share a pointer to some other objects of class B. I want to be able to change dynamically the A->B relationship, and in particular, to let all A object know when B are destroyed. I thought about emitting a “WillBeDestoyed()” signal in B dtor, and connect all my A to that signal.
Is it a viable solution ?,[/quote]Yes, it is. In fact, you are speaking about of the ROOT-dictionary-based implementation of the Qt QPointer class doc.trolltech.com/4.3/qpointer.html

[quote=“aphecetche”] Are all the signals dispatched at once ? Or do I run the risk to have some signals caught at a time, and the others at a later time ??[/quote]There is no such kind of danger yet. See some comments below.
ROOT “Signals” are not the “messages” (yet). There is no queue for signals and event loop to process them. It is still a convenient way to call the foreign class method via “internal dictionary” (CINT dictionary in the ROOT “signal” case).

From this stand point, it is useless to speak about signal “dispatching”. Each signal immediately ends up with the direct call to the slot method. That doesn’t mean one can not change this in future. ROOT accurately follows the TrollTech design. The later had introduced a so-called queued connection (see doc.trolltech.com/4.3/qt.html#Co … nType-enum )

So the most TrollTech remarks on signals are correct for the ROOT signals also (ROOT doesn’t provide any equivalent of the queued connection yet).

[quote=“http://doc.trolltech.com/4.3/signalsandslots.html”]When a signal is emitted, the slots connected to it are usually executed immediately, just like a normal function call. When this happens, the signals and slots mechanism is totally independent of any GUI event loop. Execution of the code following the emit statement will occur once all slots have returned. The situation is slightly different when using queued connections; in such a case, the code following the emit keyword will continue immediately, and the slots will be executed later.
If several slots are connected to one signal, the slots will be executed one after the other, in an arbitrary order, when the signal is emitted[/quote].

Hi Valery,

Thanks for your answer. It actually helps :wink:

BTW, I just noticed that the TQObject dtor emits the Destroyed() signal, so I guess I can simply connect to this one, instead of creating my own “WillBeDestroyed()” one, right ?

Regards,

[quote=“aphecetche”]Hi Valery,

Thanks for your answer. It actually helps :wink:

BTW, I just noticed that the TQObject dtor emits the Destroyed() signal, so I guess I can simply connect to this one, instead of creating my own “WillBeDestroyed()” one, right ?[/quote]Yes, it is correct.
See how the “big brother” does that: doc.trolltech.com/4.3/qobject.html#destroyed

Pay your attention as soon as the “destroyed” signal is emitted one can not invoke any destroyed object method anymore. The only thing you can do safely is zero the object pointer.

Hi,
as the author of this “stuff”, my 5 kopeiks.
In Qt, the Destroyed signal is used in some sort of garbage collection.
A good article on this: silmor.de/33
In fact, the same “technique” can easily be implemented in ROOT:

In ROOT one can use more advanced feature of TQObject (no analogs in Qt) ,
so called class connections. See
root.cern.ch/root/html/TQObject. … ct:Connect
This allows to connect Destroyed signal for all objects of some class.
It can be done even there no a single instance of this class exists.

Regards. Valeriy