PDA

View Full Version : signal forwarding



FelixB
26th January 2011, 11:30
Hi,

I have three Objects A,B,C. A has access to B and B has access to C. This is not(!) vice versa, so C does not now anything from B and B does not know A.

Now C emits "signal_C" and I want A to call a slot. Currently I connect "signal_C" with "slot_B". "slot_B" emits "signal_B", which is conected with "slot_A".

Is there a possibility to make this a bit easier? Can B "forward" "signal_C" somehow? Like "on receiving signal_C emit signal_B"? Or is it already best practice wih "slot_B" emitting "signal_B"?

I hope, this was not too confusing ;)
Felix

high_flyer
26th January 2011, 11:38
Can B "forward" "signal_C" somehow? Like "on receiving signal_C emit signal_B"
Yes, you can connect signals to signals.
http://doc.trolltech.com/4.7/signalsandslots.html

It is even possible to connect a signal directly to another signal. (This will emit the second signal immediately whenever the first is emitted.)

Zlatomir
26th January 2011, 11:39
Yes you can connect signal to another signal (link to documentation (http://doc.qt.nokia.com/4.7/signalsandslots.html))
Also the objects "live" in the same time, so you can directly connect signals and slot from outside of all of them (from the scope that include them)


A *prtA = new A;
B *ptrB = new B;
C *ptrC = new C;

connect(ptrC, SIGNAL(signal_fromC()), ptrA, SLOT(slot_fromA()) );
//if you are in main or outside other QObject derived class you just need to use the static connect, like this:
//QObject::connect(ptrC, SIGNAL(signal_fromC()), ptrA, SLOT(slot_fromA()) );

FelixB
27th January 2011, 09:55
Yes, you can connect signals to signals.

Thank you, that's the perfect solution! Since this is just a matter of "documentation-reading", I'd better posted in the newbie-section ;)


Also the objects "live" in the same time, so you can directly connect signals and slot from outside of all of them (from the scope that include them)

of course, but that is not possible with my code structure. A is a framework, B an extension wich can be used within the framework and C is a dialog. I don't want the extension to have any information about the framework. Also, I don't want to add a pointer to the dialog to the public interface of the extension. There is no context which has access to the framework and to the dialog. So, the signal-signal-connection is a really nice solution.