How to pause and restart the Qtimer on QT

StackTok picture StackTok · Mar 4, 2016 · Viewed 11.1k times · Source

I have a Ubuntu , and i'am working with IDE QT on C++ . I will to pause and resume the Qtimer , for exampe :

void Ordonnancer_les_taches::on_pushButton_clicked()
{

    connect(&dataTimer, SIGNAL(timeout()), this, SLOT(l_odonnancement()));
    dataTimer.start(5000);
}

How to Pause and how Restart ? give me an exmple

Answer

Stanley F. picture Stanley F. · Mar 4, 2016

Since there is no dedicated method to achieve this behaviour, you could do something like this (you may move it to a subclass PausableTime or so):

void pause() {
    int remaining = dataTimer.remainingTime();
    dataTimer.stop();
    dataTimer.setInterval(remaining);
}

void resume() {
    dataTimer.start();
}

Of course you then need to adjust the interval in your timeout slot again.