PDA

View Full Version : Signal QThread::finished() is not emited!



Szyk
13th January 2018, 17:10
Hi
I wrote simple Sqllite database app (automatic update to newer version). The only unusual thing in this app is that main thread is used for display progress bars and log output, and second thread exec update code.
Currently application works as expected but one thing does not work. After properly execution main thread is not informed about second thread finish!
My thread concept usage is as follow (example from Qt doc):


class Worker : public QObject
{
Q_OBJECT

public slots:
void doWork(const QString &parameter) {
QString result;
/* ... here is the expensive or blocking operation ... */
emit resultReady(result);
}

signals:
void resultReady(const QString &result);
};

class Controller : public QObject
{
Q_OBJECT
QThread workerThread;
public:
Controller() {
Worker *worker = new Worker;
worker->moveToThread(&workerThread);
connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
connect(this, &Controller::operate, worker, &Worker::doWork);
connect(worker, &Worker::resultReady, this, &Controller::handleResults);
workerThread.start();
}
~Controller() {
workerThread.quit();
workerThread.wait();
}
public slots:
void handleResults(const QString &);
signals:
void operate(const QString &);
};

With exception that after workerThread.start(); I emit signal which call slot in the second thread (this is working task). After my operations I expect that QThread::finished() will be emitted by the second thread. But nothing happen like that...

So my questions are as follow:
1) do I something wrong?
2) Should I emit some custom signal from second thread to inform main thread about end of processing in the second thread?!?

thanks and best wishes
Szyk Cech

high_flyer
15th January 2018, 00:32
After my operations I expect that QThread::finished() will be emitted by the second thread. But nothing happen like that...
How do you know that?
The QThread::finished() signal is connected to your Workers object deleteLater - so your code is not called by it - which is why I ask how do you know its not being fired?
One way to test would be to create a new slot in your Controller that also connects to QThread::finished() where you can have an output or put a breakpoint in it while running in a debugger.