How to create a timer that can be paused/resumed using QTimer?

349 Views Asked by At

Here's what I have so far

timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, [&slider, sliderDisplay]() {
     // increment slider value and update display, reset to min slider value once at max
});
timer->start(50);

How can I pause/resume this timer, maybe by clicking a button widget?

1

There are 1 best solutions below

0
On

there is no straight way to pause/resume. When you look at the docs you'll notice, that you can just start/stop. However if you decide to go step further, you can read the remaining time of the interval using its remainingTime() method.

As for clicking to manipulate it is rather straightforward. QPushButton offers clicked() signal, you just connect it to your own slot, that stores remaining time somewhere, then on another click inserts the value to start() method of QTimer.