Results 1 to 4 of 4

Thread: Cross-thread signal emission - does it wait for the slots?

  1. #1
    Join Date
    Oct 2008
    Posts
    71
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Cross-thread signal emission - does it wait for the slots?

    Suppose I have a background thread that at some point emits a signal from an object (the object belongs to the GUI thread!):

    Qt Code:
    1. qDebug() << "before signal emitted";
    2. emit mySignal();
    3. qDebug() << "after signal emitted";
    To copy to clipboard, switch view to plain text mode 

    This signal is connected to a slot on another object from the GUI thread:

    Qt Code:
    1. void QMyObject::mySlot() {
    2. qDebug() << "signal received";
    3. }
    To copy to clipboard, switch view to plain text mode 

    In the debug output I see the following order of messages:

    Qt Code:
    1. before signal emitted
    2. after signal emitted
    3. signal received
    To copy to clipboard, switch view to plain text mode 

    Which makes me think that when the signal is emitted, it does not wait for the slots. Although the documentation states the opposite:
    Execution of the code following the emit statement will occur once all slots have returned
    Basically in my case I would want the emitting side to wait for all the slots to process the signal. The behavior I observe is probably due to cross-thread execution, but it should be uniform for all cases, shouldn't it?

    Thanks
    Anton

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Cross-thread signal emission - does it wait for the slots?

    If you use DirectConnection, then the documentation is correct. If you use QueuedConnection, then the signal is posted to the event queue and processed when the event queue is next run.

    Signals between threads MUST be queued, as you can't do inter-thread calls.

    The appropriate connection type is automatically chosen for you based on whether the sender and receiver are in the same thread.

  3. The following user says thank you to squidge for this useful post:

    psih128 (19th January 2010)

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

    Default Re: Cross-thread signal emission - does it wait for the slots?

    Quote Originally Posted by psih128 View Post
    Which makes me think that when the signal is emitted, it does not wait for the slots. Although the documentation states the opposite:

    Execution of the code following the emit statement will occur once all slots have returned
    Did you read the next sentence of the reference?

    Quote Originally Posted by Qt docs
    The situation is slightly different when using queued connections; in such a case, the code following the emit keyword will continue immediately, and the slots will be executed later.
    If you want to block the signalling thread until all receivers return, use the Qt::BlockingQueuedConnection connection type.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    psih128 (19th January 2010)

  6. #4
    Join Date
    Oct 2008
    Posts
    71
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Cross-thread signal emission - does it wait for the slots?

    Oh thanks guys! I should pay more attention..
    Did you read the next sentence of the reference?
    Actually no.. I was so happy to find the "reason" of my problem, than I immediately proceeded to QtCentre xD

Similar Threads

  1. Replies: 9
    Last Post: 28th November 2009, 21:31
  2. Wait for a signal
    By adonel in forum Qt Programming
    Replies: 1
    Last Post: 13th May 2008, 20:53
  3. Replies: 3
    Last Post: 11th January 2008, 18:34
  4. Signal and slots
    By villy in forum Qt Programming
    Replies: 1
    Last Post: 12th January 2007, 11:10
  5. Thread(s) and socket, timer slots
    By stephdev1965 in forum Qt Programming
    Replies: 1
    Last Post: 8th November 2006, 15:04

Tags for this Thread

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.