QTimer and remainingTime()
-
QTimer is usable as single shot and multi shot timer. It provides method remainingTime() for determination of time until next shot.
Apparently the remainingTime property has been designed for single shots only. When checking in a loop there will be a count until the timer is shot. After the first shot the reminingTime() method returns always 0 indicating that the timer is overdue.Either this is a bug in current implementation of QTimer in Qt 5.4.1 or the documentation is missleading.
-
There are not many known bugs with QTimer...
https://bugreports.qt.io/browse/QTBUG-43777
https://bugreports.qt.io/browse/QTBUG-44534 -
There are not many known bugs with QTimer...
https://bugreports.qt.io/browse/QTBUG-43777
https://bugreports.qt.io/browse/QTBUG-44534Thanks for checking JIRA. I was continuing to check this issue. It is very easy to reproduce with a small application.
MyTimterTest.h#ifndef MYTIMERTEST_H #define MYTIMERTEST_H #include <QObject> #include <QTimer> class MyTimerTest : public QObject { Q_OBJECT QTimer *MyTimer1; QTimer *MyTimer2; public: explicit MyTimerTest(QObject *parent = 0); signals: public slots: void sltTimeout1(); void sltTimeout2(); }; #endif // MYTIMERTEST_H
MyTimerTest.cpp
#include "MyTimerTest.h" #include <QDebug> #include <cassert> MyTimerTest::MyTimerTest(QObject *parent) : QObject(parent) { MyTimer1 = new QTimer ( this ); bool boo = connect( MyTimer1, SIGNAL ( timeout() ), this, SLOT ( sltTimeout1() ) ); assert ( boo ); MyTimer2 = new QTimer ( this ); boo = connect( MyTimer2, SIGNAL ( timeout() ), this, SLOT ( sltTimeout2() ) ); assert ( boo ); MyTimer1->start( 5000 ); MyTimer2->start( 15000 ); } void MyTimerTest::sltTimeout1() { qDebug() << "timeout 1 "; qDebug() << "Timer 1 " << MyTimer1->isActive() << " " << MyTimer1->interval() << " " << MyTimer1->remainingTime(); qDebug() << "Timer 2 " << MyTimer2->isActive() << " " << MyTimer2->interval() << " " << MyTimer2->remainingTime(); } void MyTimerTest::sltTimeout2() { qDebug() << "timeout 2 "; qDebug() << "Timer 1 " << MyTimer1->isActive() << " " << MyTimer1->interval() << " " << MyTimer1->remainingTime(); qDebug() << "Timer 2 " << MyTimer2->isActive() << " " << MyTimer2->interval() << " " << MyTimer2->remainingTime(); }
main.cpp
#include <QCoreApplication> #include "MyTimerTest.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); MyTimerTest timerTest; return a.exec(); }
Output:
timeout 1 Timer 1 true 5000 0 Timer 2 true 15000 9987 timeout 1 Timer 1 true 5000 0 Timer 2 true 15000 4987 timeout 2 Timer 1 true 5000 0 Timer 2 true 15000 0 timeout 1 Timer 1 true 5000 0 Timer 2 true 15000 0 timeout 1 Timer 1 true 5000 0 Timer 2 true 15000 0 timeout 1 Timer 1 true 5000 0 Timer 2 true 15000 0 timeout 2 Timer 1 true 5000 0 Timer 2 true 15000 0 timeout 1 Timer 1 true 5000 0 Timer 2 true 15000 0 timeout 2 Timer 1 true 5000 0 Timer 2 true 15000 0 timeout 1 Timer 1 true 5000 0 Timer 2 true 15000 0 timeout 1 Timer 1 true 5000 0 Timer 2 true 15000 0 timeout 1 Timer 1 true 5000 0 Timer 2 true 15000 0 timeout 2 Timer 1 true 5000 0 Timer 2 true 15000 0 timeout 1 Timer 1 true 5000 0 Timer 2 true 15000 0
-
Above mentioned bug report seem to refer different issues.
Therefore, I have filed another bug report of QTBUG-46940 on JIRA. -
QTimer is usable as single shot and multi shot timer. It provides method remainingTime() for determination of time until next shot.
Apparently the remainingTime property has been designed for single shots only. When checking in a loop there will be a count until the timer is shot. After the first shot the reminingTime() method returns always 0 indicating that the timer is overdue.Either this is a bug in current implementation of QTimer in Qt 5.4.1 or the documentation is missleading.
@koahnig
Hi,I think i maybe know why.I try my best to say it clear.
oh,when we first to see theremainingTime()
,we maybe think it determination of time until next shot(it is your think).but the document says Returns the timer's remaining value in milliseconds left until the timeout,what is it the timeout?when we fisrt to run the program,time is begining plus until thetimer->start
come.and during the time is remainingTime,but when the timeout ran once.the timer is overdue(from your program begin to run to now).so,your program timer2 is show 0 after the 15000.
oh,i said ok? -
@koahnig
Hi,I think i maybe know why.I try my best to say it clear.
oh,when we first to see theremainingTime()
,we maybe think it determination of time until next shot(it is your think).but the document says Returns the timer's remaining value in milliseconds left until the timeout,what is it the timeout?when we fisrt to run the program,time is begining plus until thetimer->start
come.and during the time is remainingTime,but when the timeout ran once.the timer is overdue(from your program begin to run to now).so,your program timer2 is show 0 after the 15000.
oh,i said ok?@joeQ
Thanks for your feedback. You describe the behaviour correctly.However, the questions is if this is intended or not. If this is intended the documentation has to be clarified.
On the other hand, I do not really beleave that the behaviour is intended for continuous timers. Why bother for implementing, when it is only usable until first shot. It logical to assume that the remainingTime method gives you always the time until next timeout. It is arguable if the reset shall be done shall be done before or after sending the timeout signal. However, I would even expect that when the signal is fired the value is already indicating when the next timeout is to be expected.
Moreover, the single shot timer is started with a static function. So you do not even have to keep an object and therefore, you are not able to check lateron for remaining time.
-
it looks like documentation for Qt 5.4 [1] clearly states the behavior you're experiencing, although it may not be what you need or expect. It says "...If the timer is overdue, the returned value will be 0." So it's not a bug at least, but maybe you need to file a feature request, so for repeating timers (not single-shot) remainingTime() updates every time the timer goes off.
-
it looks like documentation for Qt 5.4 [1] clearly states the behavior you're experiencing, although it may not be what you need or expect. It says "...If the timer is overdue, the returned value will be 0." So it's not a bug at least, but maybe you need to file a feature request, so for repeating timers (not single-shot) remainingTime() updates every time the timer goes off.
That is debatable as already lined out.
However, I think it is not "natural" to have a method remainingTime which will always simply return an indication of overdue after the first shot. Let's ignore completely singleShot, because it would complicate the descriptions here.
QTimer uses a signal timeout() which is sent every time the interval exprired again. Therefore, per definition we do not have a single timeout, but multiple. The description to interval() refers also to multiple timeouts because the interval description is This property holds the timeout interval in milliseconds.As lined out in my previous post, It is the question when to reset the and restart the "counter",
As you write, it is for sure not the expected behaviour for me and I need another solution. The check for reminingTime I have implemented for checking a possible change in state. Somehow I looks to me as the timer is stopped after a certain time. I wondered if all this is connected.
-
@Jeroen3 @joeQ @Pablo-J.-Rogina and everyone who might be interested.
QTBUG-46940](https://bugreports.qt.io/browse/QTBUG-46940) on JIRA has been accepted as a bug. Check bug report more details and further follow-up.
Apparently, it was only on windows (that was what I had checked only anyhow). A patch has been created and added to DB for code review according to comments on JIRA.
-
@Jeroen3 @joeQ @Pablo-J.-Rogina and everyone who might be interested.
QTBUG-46940](https://bugreports.qt.io/browse/QTBUG-46940) on JIRA has been accepted as a bug. Check bug report more details and further follow-up.
Apparently, it was only on windows (that was what I had checked only anyhow). A patch has been created and added to DB for code review according to comments on JIRA.