Results 1 to 6 of 6

Thread: Signals/slots best practices?

  1. #1
    Join Date
    Apr 2006
    Location
    San Francisco, CA
    Posts
    186
    Qt products
    Qt4
    Platforms
    MacOS X Windows
    Thanks
    55
    Thanked 12 Times in 11 Posts

    Lightbulb Signals/slots best practices?

    Hey there,

    I've been looking at the maintainability of other people's code when they've written stuff that uses signals and slots, and have been starting to get the feeling that it's just hard to read - when the signal arrives, the code jumps to the slot function, which seems very much like a GOTO statement, and it can be tiring to find out which signal called the slot, and where the code goes to next after the slot function has completed. I'm wondering what sorts of "best practices" people use to make signal/slots code more readable and easier to follow.
    Software Engineer



  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Signals/slots best practices?

    If a slot starts, it starts from the event loop. If a slot completes, it returns to the event loop (QApplication::exec()). That's not technically always true, but that's the abstraction that is safe to assume. With "goto" there is no comming back while a slot is just a regular function call. You can use a debugger to trace such calls like any other functions. If you need more, use a signal spy.

  3. #3
    Join Date
    Mar 2006
    Location
    Mountain View, California
    Posts
    489
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 74 Times in 54 Posts

    Default Re: Signals/slots best practices?

    Withing a thread, signals are synchronous by default. Emitting a signal is like making a direct function call to the slot. If you're comfortable with function calls jumping to the code in the called function, then you should be comfortable emitting signals.

    Where it may get confusing is that the UI is event driven. When the user clicks the mouse on a QPushButton out in the UI, an event will be added to the event cue, and it will subsequently generated a clicked() signal. Thus, you will never have linear program flow.

    Don't let the name fool you, this is not at all like a "signal" in Unix. Qt signals will never interrupt your program's flow. Unless you call processEvents(), your function will complete before any signals get sent in response of UI events.

  4. #4
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11
    Thanks
    9
    Thanked 29 Times in 29 Posts

    Default Re: Signals/slots best practices?

    I've always thought of signals and slots approximately like this (only more complex). Pseudo-code:

    Qt Code:
    1. template <typeList T>
    2. class Signal {
    3. public void emit(T params) {
    4. foreach (slot in _slotList) {
    5. slot(params);
    6. }
    7. }
    8.  
    9. public void connectSlot(void slot(T)) {
    10. _slotList.pushBack(slot);
    11. }
    12.  
    13. private list<void function(T)> _slotList;
    14. }
    To copy to clipboard, switch view to plain text mode 

    You know what I mean. It's like the observer pattern, only more flexible.

    (I never thought the event loop had anything to do with it.)
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Signals/slots best practices?

    Quote Originally Posted by Michiel View Post
    (I never thought the event loop had anything to do with it.)
    It's an abstraction. If you disable direct connections, all slots will be delivered from within the event loop (that's the only safe place to disturb an event driven thread). Signals and slots as a paradigm are asynchronous and only as a simplification they are delivered in a synchronous way within a thread like Brandybuck said.

  6. #6
    Join Date
    Mar 2006
    Location
    Mountain View, California
    Posts
    489
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 74 Times in 54 Posts

    Default Re: Signals/slots best practices?

    Quote Originally Posted by Michiel View Post
    (I never thought the event loop had anything to do with it.)
    Most signals will be emitted as a response to an event. You click the mouse, for example, and you get a clicked() signal. That's why most GUI programs are said to be "event driven". The good part about Qt is that it abstacts the events with meaningful signals.

Similar Threads

  1. signals/slots across threads
    By high_flyer in forum Qt Programming
    Replies: 9
    Last Post: 13th March 2007, 00:56
  2. Qt <-> Java - Best Practices?
    By mentat in forum Qt Programming
    Replies: 6
    Last Post: 20th July 2006, 03:32
  3. Signals/Slots stopped working
    By Jimmy2775 in forum Qt Programming
    Replies: 8
    Last Post: 31st March 2006, 22:11
  4. Replies: 16
    Last Post: 7th March 2006, 16:57
  5. what datatypes allowed in signals/slots
    By smalls in forum Qt Programming
    Replies: 4
    Last Post: 18th January 2006, 18:28

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.