Timer in worker object running on separate thread
-
I wish to use the threading implementation as per "Detail Description" on https://doc.qt.io/qt-5/qthread.html for a Worker object.
My Worker implementation requires at least 1 QTimer.
I have included "// added" comments where I modified the example implementation.
I included the following:
// added a #include #include <QTimer> // added in constructor public: Worker() { timer1 = new QTimer(this); connect(timer1, SIGNAL(timeout()), this, SLOT(timer1Handler())); } // // added in doWork(..) timer1->start(10); // // added a private function private; void timer1Handler() { // do some work timer1->stop(); }
Below is the complete code
// added #include <QTimer> // Worker class class Worker : public QObject { Q_OBJECT // added public: Worker() { timer1 = new QTimer(this); connect(timer1, SIGNAL(timeout()), this, SLOT(timer1Handler())); } public slots: void doWork(const QString ¶meter) { QString result; /* ... here is the expensive or blocking operation ... */ // added timer1->start(10); emit resultReady(result); } signals: void resultReady(const QString &result); // added private; void timer1Handler() { // do some work timer1->stop(); } }; // Controller class 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 &); };
Background:
// ***
I have read extensively on aspects of Qt threading in the latest Qt documentation, and also in a large number of posts. This reading leads me to the fact that exec() needs to be executed in order to start an event loop within the thread servicing the Worker object. The event loop is required in order to service the timer used within the Worker object.Through all my searches I have not been able to find out how to call exec() using the given example. Plenty of examples exist where QThread is inherited, and a run() method override is is then overridden
My searching led me to a number of articles which render criticism against inheriting from QThread. In the examples offered, which align with what is offered at https://doc.qt.io/qt-5/qthread.html , however, do not provide any information relating to starting an event loop in this case.
*** //In my case, my Controller is a QApplication with the following in main.cpp.
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
The following summarizes the code in mainwindow.h.
#include <QThread> class MainWindow : public QMainWindow { Q_OBJECT QThread workerThread; public: MainWindow(QWidget *parent = nullptr) { 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(); } ~MainWindow() { workerThread.quit(); workerThread.wait(); } public slots: void handleResults(const QString &); signals: // various signals private: // vars Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
Is anyone able to show how to start an event loop for workerthread?
Thank you.
-
@chme said in Timer in worker object running on separate thread:
Is anyone able to show how to start an event loop for workerthread?
What doesn't work?
https://doc.qt.io/qt-5/qthread.html#start : "Begins execution of the thread by calling run()"
https://doc.qt.io/qt-5/qthread.html#run : "The default implementation simply calls exec()"
So, your worker thread should already have an event loop. -
@chme said in Timer in worker object running on separate thread:
Is anyone able to show how to start an event loop for workerthread?
Thank you.Take a look at my Qt & Threading example project
https://github.com/DeiVadder/QtThreadExampleIt covers more or less all possible ways to do parallel work in Qt-Ways and each way/path is numbered, to see the relation
There's supposed to come a video related to it, someday :D