Results 1 to 7 of 7

Thread: programming practices with connect

  1. #1
    Join Date
    Jul 2010
    Posts
    63
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Angry programming practices with connect

    (this is a very general question)

    in terms of programming practices, should i avoid bypassing parent widgets when connecting and prefer forwarding signals?

    example:
    main window
    widget1
    child1 (has a signal)
    widget2
    child2 (has a slot)

    one option would be to connect in this manner (from main window):
    connect( widget1->child1, somesignal,
    widget2->child2, someslot );

    should i avoid this kind of shortcut and prefer forwarding using signals in widget1 and widget2?

  2. #2
    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: programming practices with connect

    Well that's a matter of personal taste. Both variants are fine, but if you ask: I prefer signal forwarding. This ensures that I can change childX anytime without maintaining code in my main window.

  3. #3
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: programming practices with connect

    Think about a future. If you'll connect child's signal via manwindow's slot, in future you can use this information. For example for logging or to forwarding data via network, etc

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: programming practices with connect

    Think about a future (...)
    Exactly. If you create connection using main window slots, you can also easy perform filtering and logging on the signal's informations. Personally, I prefer to do that this way than direct connection.

  5. #5
    Join Date
    Jul 2010
    Posts
    63
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: programming practices with connect

    i don't think i completely understood you (borisbn and stampede);
    how do you forward?
    do you mean you connect a child's signal to a main window slot, and from there emitting the forward signal?
    or perhaps a chain of signals leading to destination slots?

  6. #6
    Join Date
    Aug 2010
    Posts
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: programming practices with connect

    Just in case you did not know this:
    You can connect signals to signals. That's called signal forwarding.

    So all you need to do is declare additional signals for each signal you want to forward in your widget1 and widget2 classes.

    in the constructor of widget1 you could then put code like:
    Qt Code:
    1. connect( child1, SIGNAL( somesignal() ), this, SIGNAL( forwarded_somesignal() ) );
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: programming practices with connect

    I like centralized control of signals, with a bunch of widget signals connected to a central "hub" via signal "spokes". I have had bad experiences connecting signals to one another (forming more of a web, if you will) because maintenance becomes a nightmare, and it's easy to lose track of what's responding to what.

    In my latest designs, pretty much everything goes through a central widget, and there is no crosstalk between objects. The only exception is signals that are contained entirely within an object; I don't need to know about those, and the object is free to do whatever it takes to maintain itself.

Similar Threads

  1. Best practices concerning QStandardItemModel
    By tim47 in forum Qt Programming
    Replies: 2
    Last Post: 27th January 2010, 05:29
  2. Replies: 1
    Last Post: 28th September 2008, 21:00
  3. query about best practices
    By Raajesh in forum Qt Programming
    Replies: 3
    Last Post: 13th June 2008, 18:47
  4. Signals/slots best practices?
    By gfunk in forum Qt Programming
    Replies: 5
    Last Post: 18th August 2007, 18:50
  5. Qt <-> Java - Best Practices?
    By mentat in forum Qt Programming
    Replies: 6
    Last Post: 20th July 2006, 02:32

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.