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?
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.
Code:
void QTimer::setInterval(int msec
) {
inter = msec;
if (id != INV_TIMER) { // create new timer
QObject::killTimer(id
);
// restart timer id
= QObject::startTimer(msec, Qt
::TimerType(type
));
}
}
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!
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.