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):
{
Q_OBJECT
public slots:
void doWork
(const QString ¶meter
) { /* ... here is the expensive or blocking operation ... */
emit resultReady(result);
}
signals:
void resultReady
(const QString &result
);
};
{
Q_OBJECT
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:
};
class Worker : public QObject
{
Q_OBJECT
public slots:
void doWork(const QString ¶meter) {
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 &);
};
To copy to clipboard, switch view to plain text mode
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
Bookmarks