Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [SOLVED] QTimer remainingTime() always returns 0
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QTimer remainingTime() always returns 0

Scheduled Pinned Locked Moved QML and Qt Quick
9 Posts 2 Posters 3.5k Views 1 Watching
  • 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.
  • C Offline
    C Offline
    CarolaC
    wrote on last edited by
    #1

    Hello,

    I have a problem with the remainingTime() function of QTimer. It always returns 0, even if I call it immediately after having started a timer. For example:

    @timer = new QTimer (this);
    timer->setSingleShot (true);
    timer->start (3000);
    qDebug () << "Remaining time: " << timer->remainingTime ();@

    This always prints "Remaining time: 0" on the console, though the timer still runs for 3 seconds. In the documentation, it says that the remainingTime() function only returns 0 when the timer is overdue, but that's not the case here. Am I doing something wrong?

    Thanks.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      Your code look correct to me. The only thing I can think of, is that perhaps it takes returning to the eventloop to actually have this value update. However, that is not documented, and I don't know why that would be the case here.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        CarolaC
        wrote on last edited by
        #3

        Thank you for your answer. The remainingTime() function also returns 0 when I call it later, for example 2 seconds after starting the timer, where it should give me a value around 1000 but it returns 0.

        I wanted to use this value to implement a pause button in a game, because QTimer has no pause and resume functions. I don't know other ways to realize this, so any help is appreciated.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          First of all: please file a bug report.

          Then, perhaps you can use a QElapsedTimer to figure out the run time? You may considder creating your own (temporary) class that simply wraps a QTimer and a QElapsedTimer and mimics the QTimer API. Then, you can use that as a drop-in replacement to QTimer while this bug is not fixed in Qt yet.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            CarolaC
            wrote on last edited by
            #5

            Yes, I will file a bug report.

            I didn't know about QElapsedTimer, maybe I can use it for my purpose. Thank you for the tip and also for your other suggestions!

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              If you go the own class way, you may even consider just adding a pause/resume function on that directly. Wouldn't that be a cleaner design?

              1 Reply Last reply
              0
              • C Offline
                C Offline
                CarolaC
                wrote on last edited by
                #7

                Yesterday I tried your solution with an own timer class, which uses a QElapsedTimer to calculate the remaining time and a QTimer for the timeout signal. This worked too, but the "remaining time" that I got was not really accurate (haven't checked my code for errors though).

                But I don't need it anymore, because the problem has been solved with a simple reboot. They suggested it to me when I filed the bug report. It is still strange that something like this can happen, it didn't work for me for quite some time. Now I'm glad that it has been resolved. Thank you again for your help!

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

                  Could you link the issue here please?

                  I am glad your solution worked, and it actually isn't a Qt issue. I think I would still subclass QTimer though to add the pause/resume slots.

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    CarolaC
                    wrote on last edited by
                    #9

                    This is the bug report:

                    https://bugreports.qt.io/browse/QTBUG-43777

                    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