Results 1 to 4 of 4

Thread: sending signals to ancestors

  1. #1
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question sending signals to ancestors

    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.

    Qt Code:
    1. in W3: connect(W4,SIGNAL(do_something()),this,SIGNAL(do_something()));
    2. in W2: connect(W3,SIGNAL(do_something()),this,SIGNAL(do_something()));
    3. in W1: connect(W2,SIGNAL(do_something()),this,SLOT(slot_do_something()));
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: sending signals to ancestors

    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
    Qt Code:
    1. in W3: connect(W4,SIGNAL(do_something()),this->parent()->parent(),SLOT(do_something()));
    To copy to clipboard, switch view to plain text mode 
    Looks ugly, but it works.

  3. #3
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: sending signals to ancestors

    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?

    Quote Originally Posted by marcel View Post
    Given the particular hierarchy of your widgets you can use the parent function
    Qt Code:
    1. in W3: connect(W4,SIGNAL(do_something()),this->parent()->parent(),SLOT(do_something()));
    To copy to clipboard, switch view to plain text mode 
    Looks ugly, but it works.

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: sending signals to ancestors

    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.

Similar Threads

  1. qt network performance
    By criss in forum Qt Programming
    Replies: 16
    Last Post: 24th July 2006, 09:23

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.