Results 1 to 4 of 4

Thread: QTimer::setInterval while timer running

  1. #1
    Join Date
    Jun 2012
    Posts
    202
    Thanks
    22
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QTimer::setInterval while timer running

    If setInterval(newInterval) is called while the timer is active?

    Assuming the remaining time is more than newInterval, what happens?

    1) The timer keeps running until the previous interval time expires, then interval changes to newInterval
    2) The timer keeps running and times out at newInterval later?
    3) The timer keeps running and times out based on an updated remainingTime?

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,532
    Thanked 282 Times in 277 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTimer::setInterval while timer running

    After looking to qtimer.cpp file answer 2 is closest to truth. A new object is created and starts with the period newInterval.
    Qt Code:
    1. void QTimer::setInterval(int msec)
    2. {
    3. inter = msec;
    4. if (id != INV_TIMER) { // create new timer
    5. QObject::killTimer(id); // restart timer
    6. id = QObject::startTimer(msec, Qt::TimerType(type));
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to Lesiok for this useful post:

    davethomaspilot (17th August 2022)

  4. #3
    Join Date
    Jun 2012
    Posts
    202
    Thanks
    22
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTimer::setInterval while timer running

    So, it looks like singleShot is better.






    I thought about subtracting remaining time from the new interval, but then ALL subsequent intervals will be a bit too short.

    With singleShot, I can compensate for the time between when the slot connected to timeout() executes, and when the singleShot command gets executed.

    Thanks!

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,133
    Thanks
    293
    Thanked 850 Times in 837 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTimer::setInterval while timer running

    Not sure what you are trying to accomplish - it sounds a little strange. I nearly always use single shot timers, because it is rare to for me want to have a timer that fires repeatedly until you turn it off. If you need to extend the time, you just call start() again with the new timeout period.

    I often use a single shot QTimer in conjunction with window resizing to avoid a lot of paint events when I have a complex repainting. If you let every mouse move cause a repaint, then it can be extremely slow to resize a window.

    I do something like the following:

    - create a QTimer as a member of the widget class
    - connect the timeout signal to the QWidget::update() slot
    - when the resize event is received, start the QTimer with a 250 ms timeout.
    - in the paint event, check the QTimer. If it is running, just exit without painting
    - with each subsequent resize event, restart the timer for another 250 ms.
    - once the user stops resizing, at most 250 ms later the update will trigger a repaint.

    Note that QWidget does have a QWidget::setUpdatesEnabled() method. The problem with using that instead of the above scenario is that you don't know when the user has stopped resizing - there are only resize events and nothing else - so there is no way to know when to turn updates back on.

    You can do variations of the above, where even if the timer is running you can repaint every "n" moves, or you can take a snapshot of the widget into a bitmap and bitblt it to the resized window until resizing stops and you can repaint the actual widget.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 6
    Last Post: 4th September 2014, 09:06
  2. Replies: 15
    Last Post: 4th August 2012, 19:11
  3. Replies: 0
    Last Post: 4th January 2012, 22:48
  4. timer problem(timer does not work)
    By masuk in forum Newbie
    Replies: 6
    Last Post: 14th February 2011, 05:00
  5. watchdog timer implementation based on QTimer
    By araglin in forum Qt Programming
    Replies: 3
    Last Post: 18th May 2009, 15:34

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.