Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Timer in worker object running on separate thread
QtWS25 Last Chance

Timer in worker object running on separate thread

Scheduled Pinned Locked Moved Solved General and Desktop
event loopqthreadthreadexec
4 Posts 3 Posters 2.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chme
    wrote on last edited by
    #1

    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 &parameter) {
            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.

    jsulmJ J.HilkJ 2 Replies Last reply
    0
    • C chme

      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 &parameter) {
              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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • C chme

        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 &parameter) {
                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.

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @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/QtThreadExample

        It 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


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        4
        • C Offline
          C Offline
          chme
          wrote on last edited by
          #4

          @jsulm: Thank you for your reply. The part I missed is "The default implementation simply calls exec()."

          @J.Hilk: Thank you for the great set of examples. IMHO, this should be placed in the QThread Class Documentation.

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved