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.
You can use QThread::sleep() or QThread::msleep(). Remember that waiting/sleeping will freeze your thread, which might reduce your program's performance.
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.
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.
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)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With