PDA

View Full Version : sending signals to ancestors



magland
6th October 2007, 11:11
Suppose I have widgets, W1, W2, W3, W4 where W4 is a child of W3, W3 is a child of W2, and W2 is a child of W1.... and each widget is a private member of its parent.

Now W4 emits a signal, say do_something(), that I want to connect to a slot in W1. The way I do that is to make three separate connections.



in W3: connect(W4,SIGNAL(do_something()),this,SIGNAL(do_s omething()));
in W2: connect(W3,SIGNAL(do_something()),this,SIGNAL(do_s omething()));
in W1: connect(W2,SIGNAL(do_something()),this,SLOT(slot_d o_something()));


But this means I need to declare two extra signals, and make two extra connections. And perhaps I have many such connections within my app.

Is there a better way to handle this type of situation? For example, should I use custom events?

marcel
6th October 2007, 11:21
That is called signal chaining and it is used in suche situations.
But given the particular hierarchy of your widgets you can use the parent function


in W3: connect(W4,SIGNAL(do_something()),this->parent()->parent(),SLOT(do_something()));
Looks ugly, but it works.

magland
6th October 2007, 11:37
Thanks, I had not thought of that construct... but I cannot use it because it breaks a policy that I strictly obey --- that children have no knowledge of their parents! (except in very rare situtations)

Any ideas on how to efficiently code the signal chaining?


Given the particular hierarchy of your widgets you can use the parent function


in W3: connect(W4,SIGNAL(do_something()),this->parent()->parent(),SLOT(do_something()));
Looks ugly, but it works.

marcel
6th October 2007, 11:51
Suppose I have widgets, W1, W2, W3, W4 where W4 is a child of W3, W3 is a child of W2, and W2 is a child of W1
...
but I cannot use it because it breaks a policy that I strictly obey --- that children have no knowledge of their parents

If you don't want the children to know that doesn't mean that they don't. Isn't the child-parent relation you described earlier reflexive? QObject makes it so.

And the way you do it now, connecting W3 to W2 and so on -- doesn't that mean W2 is the parent of W2?

There's no better way.