How to monitor QThread

589 views Asked by At

I am working on Qt application. There I would like to have worker thread doing some activities in background and I would like main thread to control it, restarting worker thread if it dies for some reason. I have seen finished() signal emitted by the thread so I guess I could connect slot to it.

Is that the recommended way? If not, how could I achieve that?

If I use finished() signal how can I know difference between normal termination and error?

Thanks and regards

1

There are 1 answers

2
The Quantum Physicist On

If you want to know whether you had an error or you had the process finished, simply create two signals, not one, and two slots, not one.

In the Qt documentation you have an example there, with a connection to handle the result:

connect(workerThread, &WorkerThread::resultReady, this, &MyObject::handleResults);

you're here connecting the signal resultReady with whatever method that handles the result. You create another one exactly like this, but with an error signal, and an error handler.

connect(workerThread, &WorkerThread::errorThrown, this, &MyObject::handleErrors);

Of course, another option is to simply pass a parameter with your signals/slots. It's a function call, and if you put some enum with that for the status or some int, that'll do.