[SOLVED] QTimer remainingTime() always returns 0
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);
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?
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.
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.
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.
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!
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?
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!
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.
This is the bug report: