PDA

View Full Version : Wait for the next action



maratk1n
14th May 2017, 21:39
Hi all!

Tell me, please, how can I make a delay before the next action. In this case, I still need to update the progress bar.


void ClassName::start()
{
action1();

/*

Here I need to wait a while, update the progress bar, and go further

*/

action2();
emit finished();
}


I tried to do it this way, but it does not work.



void ClassName::start()
{
emit mainLabelEmit(QString("кек"));
QTimer *timer = new QTimer(this);
timer->setSingleShot(true);
timerCount = 10;
//timer = new QTimer();
timer->start(timerCount * 1000);
while (timer->remainingTime() >= 0)
{
QString smallLabel = QString("Left %1 sec").arg(timer->remainingTime() / 1000);
//qDebug() << smallLabel;
emit smallLabelEmit(smallLabel);
if (timer->remainingTime() == 0)
{
timer->stop();
emit smallLabelEmit(QString(""));
}
else
{
emit progressBarEmit(100 - 100 * timer->remainingTime() / timerCount);
}
}

for (int i = 0; i < 7; i++)
{
emit open(i);
}
for (int i = 0; i < 7; i++)
{
emit close(i);
}
emit finished();
}


Thanks!

high_flyer
14th May 2017, 22:27
I tried to do it this way, but it does not work.

What does "not work" mean?
In what way this code is not doing what you expect?

But I think I can guess:
You have to remember that QTimer is working in an event loop.
Your while loop does not return to the main event loop thus your timer never gets started nor can it "tick".
One way to fix your code is to add processEvents() in to the while loop (and probably once just before it)