L
L
l2p2014-05-06 13:59:49
Qt
l2p, 2014-05-06 13:59:49

Qt timer: what is the best way to make a countdown?

According to the functionality of the program, it assumes that the user will have 60 seconds to complete the action. You can also pause the counter. What is the best way to organize this: QTimer or a loop with a delay per second?
QTimer didn't really understand how to pause. There are many sources, but I could not do it.
And I didn't understand the delay. QThread or are there other ways? With QThread sleep also could not do it yet.
Please help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
tugo, 2014-05-06
@l2p

DerClass::DerClass(QObject * parent)
    : QObject(parent)
    , mTimer(new QTimer(this)   
{
    connect(mTimer, SIGNAL(timeout()), this, SLOT(update()));
    mTimer->start(1000);
    mSecondsToEnd = 60;
}

DerClass::update()
{
    --mSecondsToEnd;
    if ( mSecondsToEnd > 0)
    {
        QStiring time = QString::number(mSecondsToEnd);
        ui->backTimerTextLabel->setText("Осталось " + time + " секунд" );
    }
    else
    {
        mTimer->stop();
        doWork();
    }
}

D
DancingOnWater, 2014-05-06
@DancingOnWater

Head-on solution - use QTimer along with QTimerElapsed

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question