PDA

View Full Version : Get QTimer's seconds till next timeout?



hakermania
11th February 2011, 22:10
Can I take somehow a QTimer's time till the next delay?
E.g.
when calling
timer->start(5000)
after three seconds, the value I want will be 2000

stampede
11th February 2011, 22:35
You could use QTime object, restarted each time when you call slot connected to timer's "timeout()":

void timeout_slot(){
_time.restart();
//.. other code
}
then use this object to get time elapsed from last restart (or to next update if you will):

int to_next_update(){
return _timer->interval() - _time.elapsed();
}
I don't know if there is a way to get this value using only QTimer object.

wysota
12th February 2011, 00:45
QTimer doesn't support querying its value and it wouldn't make much sense to do so anyway since the value would immediately become incorrect. QTime::elapsed() is not going to give you 2000 after 3 seconds of a 5 second timeout simply because your OS scheduler will not call you in "even" time quantums. You'll get an arbitrary value between -inf and +5000 telling you exactly nothing.

stampede
12th February 2011, 01:01
(...) because your OS scheduler will not call you in "even" time quantums
Yes, you're probably right.
But I would not post a code before checking it myself, and was working as I ( maybe incorrectly ) assumed :)

wysota
12th February 2011, 01:17
It was working because your system was not under stress. Try running 100 applications and run your program again. Compare the time computed from the elapsed() result and the time when the timer really triggers. They will differ. Especially that outputing the value of elapsed() takes time as well. There is simply no practical use from such a measurement be it correct or not. It's like doing a "tryAcquire" on a semaphore ;)

stampede
12th February 2011, 01:36
Alright, thanks for explanation wysota. Indeed, just few more running apps messed up the measurements.
@hakermania: don't use this code :)

hakermania
12th February 2011, 09:42
Ok, guys.... Well, I have a countdown updated by a timer every 1 sec. Another timer will start when the countdown ends. Then the countdown will start from the start again and when it end the starter will start again. Actually, the countdown and the second timer are independent things. I mean when I call timer->start(N) then, I call update_countdown->start(1000) and update_countdown will start counting from N/1000(seconds not milliseconds) till 0. As you understand I have some dilemma in what to do. I mean, how can I be sure that the 2 timers will be synchronized, and at the end of the countdown the timer will go through its slot?
That's why I need the time left from the timer.

stampede
12th February 2011, 10:35
Another timer will start when the countdown ends. Then the countdown will start from the start again and when it end the starter will start again. (...)
Please explain what do you want to achieve. Maybe there is an easier solution.

hakermania
12th February 2011, 11:44
1) One timer starts (starter1->start(15000)) at the same time another timer starts a countdown (shown in a progressbar) (starter2->start(1000)) which begins from 15 and then 14, 13, 12, 11, .... 1, 0. At 0, it is logical that the first timer has just finished, and an action is performed. Then the 2 timers start again.
What i want to achieve is to completely synchronize the 2 timers, so to be sure that when starter2 reaches '0', starter1 will go through its slot.
Show, there are 2 independent timers, when the starter2 finishes the starter1's slot will run. But the 2 timers are independent. How can I be sure that they'll always be synchronized?
If I am still not understood, see the attachment.5936

stampede
12th February 2011, 16:10
You don't need two timers for that, you know that you need to "update_label()" when countdown timer reaches 0. I've updated your mainWindow.cpp, works exactly like your version, just with one timer.

hakermania
12th February 2011, 19:21
Thanks, I've thought of it earlier, but, for a special reason I can't point out right now, this is impossible for my specific code!

boudie
12th February 2011, 20:57
Set counter at 15000 ms

Set timer1 with 1000 ms
When timer1 fires:
counter -= 1000
update progressbar
if counter <= 0
stop timer1
start timer2 with 1000 ms as a singleShot

When timer 2 fires:
set counter = 15000 ms
start timer1 again


Maybe not complete, but you get the picture...

wysota
13th February 2011, 00:49
Timers are unfit for measuring time. This solution still suffers from the problem I mentioned earlier. You can't measure "time" by using QTimer because QTimer is not a synchronized time source. The only reliable time source is the operating system that cooperates with the real time clock. You can use QTimer for things like countdown or such but the "time" that "elapses" has nothing to do with the "real" time.