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. QTimer and remainingTime()
QtWS25 Last Chance

QTimer and remainingTime()

Scheduled Pinned Locked Moved General and Desktop
qtimerremainingtimeqtbug-46940
10 Posts 4 Posters 8.0k 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.
  • K Offline
    K Offline
    koahnig
    wrote on last edited by koahnig
    #1

    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.

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

    joeQJ 1 Reply Last reply
    0
    • J Offline
      J Offline
      Jeroen3
      wrote on last edited by
      #2

      There are not many known bugs with QTimer...
      https://bugreports.qt.io/browse/QTBUG-43777
      https://bugreports.qt.io/browse/QTBUG-44534

      K 1 Reply Last reply
      0
      • J Jeroen3

        There are not many known bugs with QTimer...
        https://bugreports.qt.io/browse/QTBUG-43777
        https://bugreports.qt.io/browse/QTBUG-44534

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

        @Jeroen3

        Thanks 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
        

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

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          Above mentioned bug report seem to refer different issues.
          Therefore, I have filed another bug report of QTBUG-46940 on JIRA.

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

          1 Reply Last reply
          0
          • K koahnig

            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.

            joeQJ Offline
            joeQJ Offline
            joeQ
            wrote on last edited by
            #5

            @koahnig
            Hi,I think i maybe know why.I try my best to say it clear.
            oh,when we first to see the remainingTime(),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 the timer->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?

            Just do it!

            K 1 Reply Last reply
            0
            • joeQJ joeQ

              @koahnig
              Hi,I think i maybe know why.I try my best to say it clear.
              oh,when we first to see the remainingTime(),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 the timer->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?

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

              @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.

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

              1 Reply Last reply
              0
              • Pablo J. RoginaP Offline
                Pablo J. RoginaP Offline
                Pablo J. Rogina
                wrote on last edited by
                #7

                @koahnig

                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.

                [1] http://doc.qt.io/qt-5/qtimer.html#remainingTime-prop

                Upvote the answer(s) that helped you solve the issue
                Use "Topic Tools" button to mark your post as Solved
                Add screenshots via postimage.org
                Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                K 1 Reply Last reply
                0
                • Pablo J. RoginaP Pablo J. Rogina

                  @koahnig

                  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.

                  [1] http://doc.qt.io/qt-5/qtimer.html#remainingTime-prop

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

                  @Pablo-J.-Rogina

                  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.

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

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    koahnig
                    wrote on last edited by
                    #9

                    @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.

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

                    joeQJ 1 Reply Last reply
                    0
                    • K koahnig

                      @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.

                      joeQJ Offline
                      joeQJ Offline
                      joeQ
                      wrote on last edited by
                      #10

                      @koahnig you are great!

                      Just do it!

                      1 Reply Last reply
                      0

                      • Login

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