Results 1 to 3 of 3

Thread: Which design is better for signal & slots connections?

  1. #1
    Join Date
    Oct 2008
    Location
    Beijing China
    Posts
    77
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Smile Which design is better for signal & slots connections?

    I have 3 components, one of which is parent of the other 2. Say they are Parent and Child0 and Child1. All the 3 components can emit signal changed(). When one of the child emit changed(), Parent need to emit changed() and the other child need to call slot onChanged()

    My question is, which would be better in the view of design?

    design 1: connect signals of each child to the Parent's signal and slot of the other child.
    Qt Code:
    1. connect(Child0, SIGNAL(changed()), Child1, SLOT(onChanged()));
    2. connect(Child1, SIGNAL(changed()), Child0, SLOT(onChanged()));
    3. connect(Child0, SIGNAL(changed()), this, SIGNAL(changed())); // this refers to Parent
    4. connect(Child1, SIGNAL(changed()), this, SIGNAL(changed()));
    To copy to clipboard, switch view to plain text mode 

    design 2: connect signals of each child to a special slot, in which Parent will emit signal changed() and the other child call onChanged()
    Qt Code:
    1. connect(Child0, SIGNAL(changed()), this, SLOT(onChildChanged()));
    2. connect(Child1, SIGNAL(changed()), this, SLOT(onChildChanged()));
    3. ...
    4.  
    5. onChildChanged(){
    6. if(sender() == Child0)
    7. {
    8. Child1->onChanged();
    9. }
    10. else if (sender() == Child1)
    11. {
    12. Child0->onChanged();
    13. }
    14. emit changed(); // this refer to Parent
    15. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Which design is better for signal & slots connections?

    What do you want to achieve by the signals ?
    If you tell the requirements, we might be able to suggest the design properly.

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Which design is better for signal & slots connections?

    Use the first one. In the second the function is unnecessary and sender() breaks the OO design. If you will increase the numer of childs this could be less writing for you:
    Qt Code:
    1. connect(Child0, SIGNAL(changed()), this, SIGNAL(changed())); // this refers to Parent
    2. connect(Child1, SIGNAL(changed()), this, SIGNAL(changed()));
    3. //... more childs here
    4. connect(this, SIGNAL(changed()), Child0, SLOT(onChanged()));
    5. connect(this, SIGNAL(changed()), Child1, SLOT(onChanged()));
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Queued Signal Slot connections
    By SebastianBecker in forum Qt Programming
    Replies: 2
    Last Post: 24th June 2009, 20:06
  2. Replies: 2
    Last Post: 12th February 2009, 22:23
  3. Signal-Signal Connections Between Threads
    By PhilippB in forum Qt Programming
    Replies: 2
    Last Post: 15th December 2008, 18:27
  4. Replies: 2
    Last Post: 20th September 2007, 12:27
  5. Signal/slot connections of deleted items
    By Michiel in forum Newbie
    Replies: 2
    Last Post: 24th March 2006, 16:44

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
  •  
Qt is a trademark of The Qt Company.