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 28 Jun 2019, 19:16 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
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 28 Jun 2019, 19:26 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
      • F Offline
        F Offline
        fcarney
        wrote on 28 Jun 2019, 19:32 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 28 Jun 2019, 19:55
        0
        • F fcarney
          28 Jun 2019, 19:32

          @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 28 Jun 2019, 19:55 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
          • K Offline
            K Offline
            Kent-Dorfman
            wrote on 28 Jun 2019, 20:04 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 28 Jun 2019, 20:09
            3
            • K Kent-Dorfman
              28 Jun 2019, 20:04

              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 28 Jun 2019, 20:09 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
              • K Offline
                K Offline
                Kent-Dorfman
                wrote on 28 Jun 2019, 20:11 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 28 Jun 2019, 20:29
                2
                • K Kent-Dorfman
                  28 Jun 2019, 20:11

                  @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 28 Jun 2019, 20:29 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.

                  M 1 Reply Last reply 28 Jun 2019, 20:39
                  0
                  • T The178
                    28 Jun 2019, 20:29

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

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 28 Jun 2019, 20:39 last edited by
                    #9

                    @The178
                    Hi
                    On your first post
                    alt text

                    1 Reply Last reply
                    1

                    1/9

                    28 Jun 2019, 19:16

                    • Login

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