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. QThread - Using Timer instead of forever loop?

QThread - Using Timer instead of forever loop?

Scheduled Pinned Locked Moved Solved General and Desktop
qthreadtimerforever loop
8 Posts 4 Posters 3.7k 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.
  • M Offline
    M Offline
    maximus
    wrote on last edited by A Former User
    #1

    I want to have a Thread that will process an event loop, sending data back to the main Thread at a really fast pace. I still want the Thread to be responsible to signals so I don't want to use a busy loop, because in a busy loop the slots of the Tread would not get called.
    I'm just wondering if using a QTimer inside a Thread is a good solution? Will it let my Thread process signal/slot during the time it's free?

    Here is a code sample:

    Main:

      thread = new QThread;
      compuTrainer = new CompuTrainer;
      compuTrainer->moveToThread(thread);
      connect(compuTrainer, SIGNAL(dataChanged(int)), this, SLOT(dataChanged(int)) );
    

    CompuTrainer.cpp

        timerUpdateData = new QTimer(this);
        connect(timerUpdateData, SIGNAL(timeout()), this, SLOT(updateData()) );
        timerUpdateData->start(150);
    
    
    void CompuTrainer::updateData() {
        emit dataChanged(someDataHere);
    }
    

    Edit: Coded fully and seems to be working, just want to make sure using QTimer inside the Thread is okay.. Thanks


    Free Indoor Cycling Software - https://maximumtrainer.com

    K 1 Reply Last reply
    0
    • M maximus

      I want to have a Thread that will process an event loop, sending data back to the main Thread at a really fast pace. I still want the Thread to be responsible to signals so I don't want to use a busy loop, because in a busy loop the slots of the Tread would not get called.
      I'm just wondering if using a QTimer inside a Thread is a good solution? Will it let my Thread process signal/slot during the time it's free?

      Here is a code sample:

      Main:

        thread = new QThread;
        compuTrainer = new CompuTrainer;
        compuTrainer->moveToThread(thread);
        connect(compuTrainer, SIGNAL(dataChanged(int)), this, SLOT(dataChanged(int)) );
      

      CompuTrainer.cpp

          timerUpdateData = new QTimer(this);
          connect(timerUpdateData, SIGNAL(timeout()), this, SLOT(updateData()) );
          timerUpdateData->start(150);
      
      
      void CompuTrainer::updateData() {
          emit dataChanged(someDataHere);
      }
      

      Edit: Coded fully and seems to be working, just want to make sure using QTimer inside the Thread is okay.. Thanks

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @maximus
      I would think the different sleep methods are for that.

      Vote the answer(s) that helped you to solve your issue(s)

      M 1 Reply Last reply
      0
      • K koahnig

        @maximus
        I would think the different sleep methods are for that.

        M Offline
        M Offline
        maximus
        wrote on last edited by
        #3

        @koahnig
        While the Thread is sleeping, can it still receive signals?
        Also, I didn't subclass QThread, i'm just using a vanilla Thread so I can't use sleep directly.


        Free Indoor Cycling Software - https://maximumtrainer.com

        K 1 Reply Last reply
        0
        • M maximus

          @koahnig
          While the Thread is sleeping, can it still receive signals?
          Also, I didn't subclass QThread, i'm just using a vanilla Thread so I can't use sleep directly.

          K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          @maximus
          I have not a lot of experience with QThread. However, I would assume that with the msleep method you can get a similar effect.
          The sleep methods are static.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mcosta
            wrote on last edited by
            #5

            Hi,

            Using QTimer inside a Thread is IMO the best solution to achieve your goal.

            Once your problem is solved don't forget to:

            • Mark the thread as SOLVED using the Topic Tool menu
            • Vote up the answer(s) that helped you to solve the issue

            You can embed images using (http://imgur.com/) or (http://postimage.org/)

            1 Reply Last reply
            2
            • M Offline
              M Offline
              maximus
              wrote on last edited by
              #6

              Thanks! [Solved]

              Just wondering why I could not google this solution, found many subclass QThread solution that seems really complicated compared to this one.


              Free Indoor Cycling Software - https://maximumtrainer.com

              ? 1 Reply Last reply
              0
              • M Offline
                M Offline
                mcosta
                wrote on last edited by
                #7

                Hi,

                subclassing thread was probably the first solution used in the past but now the one with moveToThread() is the best one and also the simpler to manage.

                BTW, you can refer to this to figure out what is the best solution depending to what do you need

                Once your problem is solved don't forget to:

                • Mark the thread as SOLVED using the Topic Tool menu
                • Vote up the answer(s) that helped you to solve the issue

                You can embed images using (http://imgur.com/) or (http://postimage.org/)

                1 Reply Last reply
                0
                • M maximus

                  Thanks! [Solved]

                  Just wondering why I could not google this solution, found many subclass QThread solution that seems really complicated compared to this one.

                  ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #8

                  @maximus Hi! Have a look at this blog post: https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

                  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