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. Is QThread doc correct?
Forum Updated to NodeBB v4.3 + New Features

Is QThread doc correct?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 283 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.
  • Q Offline
    Q Offline
    qwe3
    wrote on last edited by qwe3
    #1

    Hi,

    In QThread docs, in detailed description section I see an example:

    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 &);
    };
    

    And it is correct?

    Qt::QueuedConnection	- The slot is invoked when control returns to the event loop of the receiver's thread. 
    The slot is executed in the receiver's thread.
    

    I think we should add Qt::QueuedConnection to:

            connect(this, &Controller::operate, worker, &Worker::doWork);
            connect(worker, &Worker::resultReady, this, &Controller::handleResults);
    

    I would like to execute doWork() method in other thread, so for me the correct is:

            connect(this, &Controller::operate, worker, &Worker::doWork, Qt::QueuedConnection);
            connect(worker, &Worker::resultReady, this, &Controller::handleResults, Qt::QueuedConnection);
    
    J.HilkJ 1 Reply Last reply
    0
    • Q qwe3

      Hi,

      In QThread docs, in detailed description section I see an example:

      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 &);
      };
      

      And it is correct?

      Qt::QueuedConnection	- The slot is invoked when control returns to the event loop of the receiver's thread. 
      The slot is executed in the receiver's thread.
      

      I think we should add Qt::QueuedConnection to:

              connect(this, &Controller::operate, worker, &Worker::doWork);
              connect(worker, &Worker::resultReady, this, &Controller::handleResults);
      

      I would like to execute doWork() method in other thread, so for me the correct is:

              connect(this, &Controller::operate, worker, &Worker::doWork, Qt::QueuedConnection);
              connect(worker, &Worker::resultReady, this, &Controller::handleResults, Qt::QueuedConnection);
      
      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @qwe3 said in Is QThread doc correct?:

      And it is correct?

      it is.

      The default parameter for QObject::Connect() is Qt::AutoConnection, which will be automatically Qt::QeueudConnection if its across threads.

      You very rarely need to force a connection type


      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
      6
      • Q Offline
        Q Offline
        qwe3
        wrote on last edited by
        #3

        @J-Hilk ... Thank you very much! I thought that default is DirectConnection. Thank you! :)

        jsulmJ 1 Reply Last reply
        1
        • Q qwe3

          @J-Hilk ... Thank you very much! I thought that default is DirectConnection. Thank you! :)

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

          @qwe3 said in Is QThread doc correct?:

          I thought that default is DirectConnection

          Take a look at the connect() signature: https://doc.qt.io/qt-5/qobject.html#connect-2

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

          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