Results 1 to 13 of 13

Thread: Get QTimer's seconds till next timeout?

  1. #1
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Get QTimer's seconds till next timeout?

    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
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Get QTimer's seconds till next timeout?

    You could use QTime object, restarted each time when you call slot connected to timer's "timeout()":
    Qt Code:
    1. void timeout_slot(){
    2. _time.restart();
    3. //.. other code
    4. }
    To copy to clipboard, switch view to plain text mode 
    then use this object to get time elapsed from last restart (or to next update if you will):
    Qt Code:
    1. int to_next_update(){
    2. return _timer->interval() - _time.elapsed();
    3. }
    To copy to clipboard, switch view to plain text mode 
    I don't know if there is a way to get this value using only QTimer object.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Get QTimer's seconds till next timeout?

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Get QTimer's seconds till next timeout?

    (...) 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

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Get QTimer's seconds till next timeout?

    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
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Get QTimer's seconds till next timeout?

    Alright, thanks for explanation wysota. Indeed, just few more running apps messed up the measurements.
    @hakermania: don't use this code

  7. #7
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Get QTimer's seconds till next timeout?

    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.
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  8. #8
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Get QTimer's seconds till next timeout?

    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.

  9. #9
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Get QTimer's seconds till next timeout?

    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.2timers_example.zip
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  10. #10
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Get QTimer's seconds till next timeout?

    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.
    Attached Files Attached Files

  11. The following user says thank you to stampede for this useful post:

    hakermania (12th February 2011)

  12. #11
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Get QTimer's seconds till next timeout?

    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!
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  13. #12
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Get QTimer's seconds till next timeout?

    Qt Code:
    1. Set counter at 15000 ms
    2.  
    3. Set timer1 with 1000 ms
    4. When timer1 fires:
    5. counter -= 1000
    6. update progressbar
    7. if counter <= 0
    8. stop timer1
    9. start timer2 with 1000 ms as a singleShot
    10.  
    11. When timer 2 fires:
    12. set counter = 15000 ms
    13. start timer1 again
    To copy to clipboard, switch view to plain text mode 

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

  14. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Get QTimer's seconds till next timeout?

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. The following 2 users say thank you to wysota for this useful post:

    frankiefrank (14th October 2012), hakermania (18th February 2011)

Similar Threads

  1. QTimer in QThread doesn't start or timeout
    By Boron in forum Qt Programming
    Replies: 9
    Last Post: 21st October 2011, 13:51
  2. How to wait till QNetworkReply gives a result
    By Furkan in forum Qt Programming
    Replies: 3
    Last Post: 23rd November 2010, 10:00
  3. QTimer in QThread - Seem to be always 5 seconds
    By bazmrl in forum Qt Programming
    Replies: 2
    Last Post: 13th August 2010, 09:53
  4. QTimer delayed timeout
    By DavidY in forum Qt Programming
    Replies: 4
    Last Post: 10th December 2009, 21:34
  5. Replies: 2
    Last Post: 9th September 2009, 00:26

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.