Results 1 to 2 of 2

Thread: "Blocking" a QThread until a signal has arrived

  1. #1
    Join Date
    Jul 2012
    Thanked 15 Times in 14 Posts
    Qt products

    Question "Blocking" a QThread until a signal has arrived

    I have a QThread running, which receives signals from a QProcess (asynchronously). Now I want to "block" a function executing in the context of that thread, until the same thread has received a certain signal from the process.

    I would imagine it something like this:

    Qt Code:
    1. class A
    2. {
    3. bool ready = false;
    4. public:
    6. void get_something()
    7. {
    8. while(!ready)
    9. QThread::currentThread()->[B]get_event_loop()[/B]->processEvents();
    11. //tadaa! now we have what we were waiting for!
    12. //...
    13. return xyz;
    14. }
    16. public slots:
    17. //this slot is connected to the stdout output of a QProcess
    18. void set_something(...)
    19. {
    20. ready = true;
    21. //...
    22. }
    24. };
    To copy to clipboard, switch view to plain text mode 

    (Note that both get_something() and set_something() live in the same thread)

    Problem is that QThread does not have a method to access its QEventLoop!

    Any ideas?


    As an aside: Currently the GUI thread is the only thread involved (imagine get_something() being something like a buttonpress-event). So I could do QApplicatoin:rocessAllEvents(). But this got me wondering, what if it is not the GUI thread, but any generic QThread? How to access its event loop?

  2. #2
    Join Date
    Jan 2006
    Graz, Austria
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Unix/X11 Windows

    Default Re: "Blocking" a QThread until a signal has arrived

    Maybe there is an easier way: QIODevice, the base class of QProcess has a waitForReadyRead() function that can be used to blockingly wait for the readyRead() signal emit.

    Alternatively you could start the thread's event loop when you "wait" and stop it when you are done
    Qt Code:
    1. void get_something()
    2. {
    3. // wait in event loop
    4. exec();
    5. }
    7. void set_something()
    8. {
    9. // stop event loop
    10. quit();
    11. }
    To copy to clipboard, switch view to plain text mode 

    And for completeness: the "missing" function in your original code is QThread::eventDispatcher().


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

    tuli (14th April 2019)

Similar Threads

  1. Replies: 1
    Last Post: 20th November 2015, 11:02
  2. Replies: 3
    Last Post: 16th March 2015, 08:31
  3. Replies: 4
    Last Post: 26th June 2008, 19:41
  4. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 20:05
  5. Signal defined in "a.h" can not emit in "b.cpp"
    By Shawn in forum Qt Programming
    Replies: 9
    Last Post: 21st May 2007, 17:55

Tags for this Thread


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.