How to use sleep function in Qt(C++)? | Qt


How to use sleep in Qt(C++)?

These are two method to use sleep() in Qt(C++).

sleep() by QThread

QThread has a function named by sleep().
It is too simple to use. Just call sleep() where you want.

here is example:

#include <QThread>

//.... do something .....
QThread::sleep(10)  // Wait for 10 sec
//.... do something .....

sleep() with QEventLoop

QThread::sleep() can only use “sec”.
But, QEventLoop can use “msec”. QEventLoop case is useful to control drivers, etc..
How to ? The combination with QEventLoop and QTimer.

here is example:

#include <QTimer>
#include <QEventLoop>

//.... do something .....
QEventLoop loop;
QTimer::singleshot(10, &amp;amp;amp;amp;amp;amp;amp;amp;loop, SLOT(quit());
loop.exec();
// Process after 10 msec
//.... do something .....

By the way。you can use Sleep() of C++, of course.

//.... do something .....
Sleep(10) // Wait for 10 sec
//.... do something .....

タイトルとURLをコピーしました