PDA

View Full Version : passing SIGNAL from a object contained in another... is there a nicer way?



pir
17th May 2006, 19:31
Hi!

I'm just wonder how people normally do when they have a instance A lying in a instance B which lies in Frame C. Instance D lies in C and needs signals from A. But since A is not known by C or D, the signal from A must be passed by B... do I need to do like this:
( excuse the bad indentation on the code, I've tried to prettify it )

who knows who:
A<------B<------- [C]--------->D

propagation of signal a1:
[A: send SIGNAL a1 ]->[B: recieve in SLOT, pass SIGNAL b1 (saying the same thing)]-------------->D[recieves SIGNAL b1 which actually is a1]




/** class A ******************/
class A{
signals:
void a1();
}; class A

/** class B *******************/
class B{

/** connect in constructor */
B( )
{
connect ( &a, SIGNAL(a1()), this, SLOT(pass_signalA1()));
}// B ( )

slots:

/** a slot only passing the signal */
void pass_signalA1()
{
emit b1();
}// pass_signalA1()

signals:

/** the signal, actually the a1 signal passed on */
void b1();
A a;
}; // class B

/*** class C **************/
class C{

/** connect B and D */
C ( )
{
connect ( &b, signal(b1()), &d, SLOT(Dslot()));
}// C( )

B b;
D d;
}; //class C

/*** class D *****/
class D{

/** slot recieving the signal b1 */
slots:
void Dslot();

};//class D

Maybe I should show some code instead, but I give this a chance.

jacek
17th May 2006, 19:44
You don't have to implement a slot to pass the signal, you can do this instead:
connect( &a, SIGNAL( a1() ), this, SIGNAL( b1() ) );Now whenever "this" receives the a1() signal, it will emit b1().

pir
17th May 2006, 21:01
nice, thanks! So it was worth the trouble writing this message after all.