PDA

View Full Version : relaying signals



Cruz
4th April 2010, 15:52
Hello!

Here is the situation. Object A has a B and B has a C (A -> B -> C). C emits signals that are actually meant for A, but A doesn't know about C, so I can't connect() the signals from C to A in A. B knows about C, but it doesn't know about A, so I can't connect() C to A there either. For now the only solution I see is connecting C to B and reemitting the signal from B to A. Isn't there something better I can do?

Thanks
Cruz

borisbn
5th April 2010, 04:34
You do not need to reemit signal yourself. You can connect signal of C to signal of B. And if you connected signal of B to A's slot, you will automatically receive emit of C's signal


B::B()
{
connect( &c, SIGNAL( c_signal_signature ), SIGNAL( b_signal_signature ) );
}

A::A()
{
connect( &b, SIGNAL( b_signal_signature ), SLOT( a_slot_signature ) );
}

aamer4yu
5th April 2010, 17:04
Here is the situation. Object A has a B and B has a C (A -> B -> C).
As far as I understand its something like -

class A
{
B objB;
};

class B
{
C objC;
}

class C
{
}

If thats the case, you need to provide a function in B to return objC. say C* B::getObjC()
Then in class A or whereever you can connect

connect(objB->getObjC(), SIGNAL(), this, SLOT());
connect(objA->getObjB()->getObjC(), SIGNAL(), objA, SLOT());


If above is not ur required hierarchy, please mention with class structure.

borisbn
5th April 2010, 19:53
aamer4yu, Cruz said:

but A doesn't know about C
I understood it that A knows NOTHING about C.
that's why connecting C's signal to B's signal, and connecting B's signal to A's slot - it seems to me the right idea. Isn't it ?

Cruz
5th April 2010, 19:58
"You can connect signal of C to signal of B." - yeah thanks, I wasn't aware of this. This solution is satisfactory.

aamer4yu
6th April 2010, 05:47
I understood it that A knows NOTHING about C.
I even didnt say A knows anything about C... but in some way A knows that it has to perform some operation based on a given signal.
It depends on where you perform the connect operation. If you connect in main window class,, then I guess it may know about A,B and C.

I just gave a way where you need not put extra signal in class B. Just imagine if A->B->C->D->E->F->G was the case, and you wanted to emit signal from G and process in A... what technique would you have used ?
Would you put a signal all the way from B to F ??

Cruz
6th April 2010, 09:50
Would you put a signal all the way from B to F ??

Good point. However, the solution you suggested requires that A knows about C, or about G in the case of the extreme example.


connect(objA->getObjB()->getObjC(), SIGNAL(), objA, SLOT());

requires A to include C's header so that it knows about its signals, right? And that's also not very pretty, because B was supposed to hide C from A. So if neither this, nor the signal relaying are perfectly clean solutions for this kind of problem, what else can be done?

aamer4yu
6th April 2010, 17:59
requires A to include C's header so that it knows about its signals, right?
Not necessarily,,, you can make the connection from your main class,,, which knows about A,B and C.
Also if you are making connection inside A, then A in someway knows about the signal, doesnt it ?? atleast the name and signature :rolleyes: