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. how to pass periodically changed variables in new thread?
Qt 6.11 is out! See what's new in the release blog

how to pass periodically changed variables in new thread?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 2.1k Views 1 Watching
  • 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.
  • S Offline
    S Offline
    SpartaWHY117
    wrote on last edited by
    #1

    in main thread, i have used timers to do some calculation, and some(a little more) variables changed periodically, and because the output those variables to stdout cost time, so i want use new thread to out put them.

    but what is the correct way to achieve this? i have read this article How To Really, Truly Use QThreads; The Full Explanation. it seems like to pass parameters at first into the work class, but still don't know how to pass changed variable into thread for doing something.

    Accroding to qt document, Customer a new thread or new work class , which one is the correct way to achieve my goal?

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      So first you create your worker object, for example:

      class Writer : public QObject
      {
          Q_OBJECT
      public slots:
          void write(const QString& stuff)
          {
              std::cout << stuff.toUtf8().constData();
              std::cout.flush();
          }
      };
      

      Then you create a thread object, worker object, move the worker to that thread and start the thread:

      QThread* thread = new QThread(some_parent);                        //create the thread
      Writer* writer = new Writer;                                       //create the worker (no parent so you can move it)
      writer->moveToThread(thread);                                      //move worker to the thread
      connect(thread, &QThread::finished, writer, &Writer::deleteLater); //delete the worker when thread quits
      thread->start();                                                   //start the thread
      

      Now connect the worker to some signal that you emit when you need to write something:

      connect(somethingThatProducesMessages, &SomeObject::messageProduced, writer, &Writer::write);
      

      Then you just need to stop the thread at some point:

      thread->quit();
      thread->wait();
      
      S 1 Reply Last reply
      4
      • Chris KawaC Chris Kawa

        So first you create your worker object, for example:

        class Writer : public QObject
        {
            Q_OBJECT
        public slots:
            void write(const QString& stuff)
            {
                std::cout << stuff.toUtf8().constData();
                std::cout.flush();
            }
        };
        

        Then you create a thread object, worker object, move the worker to that thread and start the thread:

        QThread* thread = new QThread(some_parent);                        //create the thread
        Writer* writer = new Writer;                                       //create the worker (no parent so you can move it)
        writer->moveToThread(thread);                                      //move worker to the thread
        connect(thread, &QThread::finished, writer, &Writer::deleteLater); //delete the worker when thread quits
        thread->start();                                                   //start the thread
        

        Now connect the worker to some signal that you emit when you need to write something:

        connect(somethingThatProducesMessages, &SomeObject::messageProduced, writer, &Writer::write);
        

        Then you just need to stop the thread at some point:

        thread->quit();
        thread->wait();
        
        S Offline
        S Offline
        SpartaWHY117
        wrote on last edited by
        #3

        @Chris-Kawa thanks for your answer. it really helps me!

        1 Reply Last reply
        0

        • Login

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