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 it necessary to restart a timer with a timeout() signal set at constant intervals?

Is it necessary to restart a timer with a timeout() signal set at constant intervals?

Scheduled Pinned Locked Moved Solved General and Desktop
qtimerqtime
9 Posts 5 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.
  • T Offline
    T Offline
    The178
    wrote on last edited by The178
    #1

    Curious about this:
    I have a mainwindow class with a private variable object QTimer *heartbeat .
    In mainwindow constructor, I have the following code:

    heartbeat = new QTimer(this); //line1
    heartbeat->setTimerType(Qt::PreciseTimer);  //line2
    heartbeat->setInterval(500);  //line3
    connect(heartbeat, SIGNAL(timeout(void)), this, SLOT(hbTimer(void))); //line4
    heartbeat->start(); //line5
    

    The last line of hbTimer() is heartbeat->start(); this, to me, means that the timer is restarted and hence we can accrue the 0.5 second timeout interval and execute hbTimer() again. The thing is, I thought the way the codeblock above is written will ensure that hbTimer() is executed every 0.5 seconds without us having to actually tell it to restart inside the hbTimer(). Do we need to restart heartbeat inside hbTimer() or does the code block ensure that it will be executed every 500ms? Meaning, if hbTimer() didn't restart heartbeat, would we only have one occurrence of a half second timeout? I thought that by using setInterval() we tell it to execute the SLOT every 500ms regardless of that being from 0 to 0.5 or 2.5 to 2.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Unless you set the single shot property to true, the timer will timeout at each interval until you stop it.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      4
      • fcarneyF Offline
        fcarneyF Offline
        fcarney
        wrote on last edited by
        #3

        @The178 said in Is it necessary to restart a timer with a timeout() signal set at constant intervals?:

        connect(heartbeat, SIGNAL(timeout(void)), this, SLOT(hbTimer(void)));

        Is the heartbeat not occurring? If not check that your connect didn't fail by checking the output of the console. If it failed for some reason you should see something to that effect.

        C++ is a perfectly valid school of magic.

        T 1 Reply Last reply
        0
        • fcarneyF fcarney

          @The178 said in Is it necessary to restart a timer with a timeout() signal set at constant intervals?:

          connect(heartbeat, SIGNAL(timeout(void)), this, SLOT(hbTimer(void)));

          Is the heartbeat not occurring? If not check that your connect didn't fail by checking the output of the console. If it failed for some reason you should see something to that effect.

          T Offline
          T Offline
          The178
          wrote on last edited by The178
          #4

          Hi @fcarney, it is occurring. And it also occurs with the same behavior when I comment out heartbeat.start() inside hbTimer() which is why I got curious if I actually need that line or if there is some hidden advantage of restarting the timer like this that I am missing. I thought hbTimer() will be reoccurring every 0.5seconds regardless of what goes on inside hbTimer (assuming we don't make any changes to heartbeat inside it).

          Hi @SGaist, I don't think it's set to single shot property.

          Unless you set the single shot property to true, the timer will timeout at each interval until you stop it.

          Does the bold part mean from, eg. 0-0.5, 3.5-4, etc. (not only from 0 to 0.5s)?
          Thank you

          1 Reply Last reply
          0
          • Kent-DorfmanK Offline
            Kent-DorfmanK Offline
            Kent-Dorfman
            wrote on last edited by
            #5

            you most certainly don't want to have to restart the timer at every periodic interval because that would introduce drift.

            the timeouts are guaranteed to occur at base_time+(n * interval) for the n(th) occurence. If you restart the timer in your slot then every iteration you would add some small amount of error.

            T 1 Reply Last reply
            3
            • Kent-DorfmanK Kent-Dorfman

              you most certainly don't want to have to restart the timer at every periodic interval because that would introduce drift.

              the timeouts are guaranteed to occur at base_time+(n * interval) for the n(th) occurence. If you restart the timer in your slot then every iteration you would add some small amount of error.

              T Offline
              T Offline
              The178
              wrote on last edited by
              #6

              @Kent-Dorfman Ahh, perfect that answers my question.
              I am taking over someone else's code and there's a lot going on. I'll consult the more experienced coworker to see if there is any purpose for resetting this timer. Can you think of a reason when this type of a reset would be desirable?

              1 Reply Last reply
              0
              • Kent-DorfmanK Offline
                Kent-DorfmanK Offline
                Kent-Dorfman
                wrote on last edited by
                #7

                @The178 said in Is it necessary to restart a timer with a timeout() signal set at constant intervals?:

                Can you think of a reason when this type of a reset would be desirable?

                If you only care about the time interval BETWEEN events...such as do some proccessing and THEN delay for some period of time.

                T 1 Reply Last reply
                2
                • Kent-DorfmanK Kent-Dorfman

                  @The178 said in Is it necessary to restart a timer with a timeout() signal set at constant intervals?:

                  Can you think of a reason when this type of a reset would be desirable?

                  If you only care about the time interval BETWEEN events...such as do some proccessing and THEN delay for some period of time.

                  T Offline
                  T Offline
                  The178
                  wrote on last edited by
                  #8

                  @Kent-Dorfman You rock, thank you. Not sure how to set the question to answered, but it has now been answered haha.

                  mrjjM 1 Reply Last reply
                  0
                  • T The178

                    @Kent-Dorfman You rock, thank you. Not sure how to set the question to answered, but it has now been answered haha.

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @The178
                    Hi
                    On your first post
                    alt text

                    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