Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a pause/wait function using Qt?

Tags:

c++

sleep

wait

qt

I'm playing around with Qt, and I want to create a simple pause between two commands. However it won't seem to let me use Sleep(int mili);, and I can't find any obvious wait functions.

I am basically just making a console application to test some class code which will later be included in a proper Qt GUI, so for now I'm not bothered about breaking the whole event-driven model.

like image 548
Zac Avatar asked Sep 20 '10 15:09

Zac


People also ask

How do I add a wait in Qt?

You can use QThread::sleep() or QThread::msleep(). Remember that waiting/sleeping will freeze your thread, which might reduce your program's performance.

How do you set a delay in Qt?

and you call it by doing this: Sleeper::usleep(10); Sleeper::msleep(10); Sleeper::sleep(10); This would give you a delay of 10 microseconds, 10 milliseconds or 10 seconds, accordingly.

How do you make a wait in C++?

Wait for seconds using delay() function in C++ We can use the delay() function to make our programs wait for a few seconds in C++. The delay() function in c++ is defined in the 'dos. h' library. Therefore, it is mandatory to include this header file to use the delay function() in our program.


1 Answers

I wrote a super simple delay function for an application I developed in Qt.

I would advise you to use this code rather than the sleep function as it won't let your GUI freeze.

Here is the code:

void delay() {     QTime dieTime= QTime::currentTime().addSecs(1);     while (QTime::currentTime() < dieTime)         QCoreApplication::processEvents(QEventLoop::AllEvents, 100); } 

To delay an event by n seconds - use addSecs(n).

like image 153
Kartik Sharma Avatar answered Sep 21 '22 13:09

Kartik Sharma