Is it necessary to restart a timer with a timeout() signal set at constant intervals?
-
wrote on 28 Jun 2019, 19:16 last edited by The178
Curious about this:
I have a mainwindow class with a private variable objectQTimer *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. -
Hi,
Unless you set the single shot property to true, the timer will timeout at each interval until you stop it.
-
wrote on 28 Jun 2019, 19:32 last edited by
@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.
-
@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.
wrote on 28 Jun 2019, 19:55 last edited by The178Hi @fcarney, it is occurring. And it also occurs with the same behavior when I comment out
heartbeat.start()
insidehbTimer()
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 -
wrote on 28 Jun 2019, 20:04 last edited by
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.
-
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.
wrote on 28 Jun 2019, 20:09 last edited by@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? -
wrote on 28 Jun 2019, 20:11 last edited by
@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.
-
@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.
wrote on 28 Jun 2019, 20:29 last edited by@Kent-Dorfman You rock, thank you. Not sure how to set the question to answered, but it has now been answered haha.
-
@Kent-Dorfman You rock, thank you. Not sure how to set the question to answered, but it has now been answered haha.
@The178
Hi
On your first post
1/9