View Full Version : "Blocking" a QThread until a signal has arrived

12th April 2019, 21:08
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:

class A
bool ready = false;

void get_something()

//tadaa! now we have what we were waiting for!
return xyz;

public slots:
//this slot is connected to the stdout output of a QProcess
void set_something(...)
ready = true;


(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::processAllEvents(). But this got me wondering, what if it is not the GUI thread, but any generic QThread? How to access its event loop?

13th April 2019, 09:41
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

void get_something()
// wait in event loop

void set_something()
// stop event loop

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