How to create pause / wait function using Qt?

I play with Qt , and I want to create a simple pause between two teams. However, it seems that I will not use Sleep(int mili); , and I cannot find any obvious wait functions.

I basically just create a console application for testing class code, which will later be included in the correct Qt GUI, so for now I'm not worried about breaking the entire event-driven model.

+65
c ++ sleep wait qt
Sep 20 '10 at 15:07
source share
12 answers

This previous question mentions the use of qSleep() , which is in the QtTest module. To avoid overhead binding in the QtTest module, looking at the source of this function, you can simply create your own copy and call it. It uses definitions to call Windows Sleep() or Linux nanosleep() .

 #ifdef Q_OS_WIN #include <windows.h> // for Sleep #endif void QTest::qSleep(int ms) { QTEST_ASSERT(ms > 0); #ifdef Q_OS_WIN Sleep(uint(ms)); #else struct timespec ts = { ms / 1000, (ms % 1000) * 1000 * 1000 }; nanosleep(&ts, NULL); #endif } 
+29
Sep 20 '10 at 15:54
source share

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

I would advise you to use this code, not the sleep function, as it will not allow your GUI to freeze.

Here is the code:

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

To delay the event for n seconds - use addSecs(n) .

+122
Jul 14 '12 at 21:14
source share

Starting with Qt5 we can also use

QThread Static Public Members

 void msleep(unsigned long msecs) void sleep(unsigned long secs) void usleep(unsigned long usecs) 
+56
Mar 19 '15 at 16:32
source share

Small +1 to kshark27 answer to make it dynamic:

 #include <QTime> void delay( int millisecondsToWait ) { QTime dieTime = QTime::currentTime().addMSecs( millisecondsToWait ); while( QTime::currentTime() < dieTime ) { QCoreApplication::processEvents( QEventLoop::AllEvents, 100 ); } } 
+31
Jan 03 '14 at 0:25
source share

We used below class -

 class SleepSimulator{ QMutex localMutex; QWaitCondition sleepSimulator; public: SleepSimulator::SleepSimulator() { localMutex.lock(); } void sleep(unsigned long sleepMS) { sleepSimulator.wait(&localMutex, sleepMS); } void CancelSleep() { sleepSimulator.wakeAll(); } }; 

QWaitCondition is designed to coordinate the expectation of a mutex between different threads. But what does this work is the wait method has a timeout on it. When called like that, it functions just like a sleep function, but it uses the Qt event loop to synchronize. Thus, no other events or user interface are blocked, as a function of the normal operation of Windows.

As a bonus, we added the CancelSleep function, so that another part of the program cancels the "sleep" function.

What we liked about this is that it is lightweight, reusable and completely autonomous.

QMutex: http://doc.qt.io/archives/4.6/qmutex.html

QWaitCondition: http://doc.qt.io/archives/4.6/qwaitcondition.html

+8
Sep 20 '10 at 23:46
source share

Since you are trying to "check some class code", I would really recommend learning to use QTestLib . It provides the QTest namespace and the QtTest module , which contains a number of useful functions and objects, including QSignalSpy , which you can use to verify that certain signals are emitted.

Since you will eventually integrate with the full GUI, using QTestLib and testing without sleep or waiting will give you a more accurate test - one that better reflects true usage patterns. But, if you decide not to follow this route, you can use QTestLib :: qSleep to accomplish what you requested.

Since you just need to pause between starting the pump and turning it off, you can easily use the one-shot timer:

 class PumpTest: public QObject { Q_OBJECT Pump &pump; public: PumpTest(Pump &pump):pump(pump) {}; public slots: void start() { pump.startpump(); } void stop() { pump.stoppump(); } void stopAndShutdown() { stop(); QCoreApplication::exit(0); } void test() { start(); QTimer::singleShot(1000, this, SLOT(stopAndShutdown)); } }; int main(int argc, char* argv[]) { QCoreApplication app(argc, argv); Pump p; PumpTest t(p); t.test(); return app.exec(); } 

But qSleep() will definitely be easier if all you are interested in is checking a couple of things on the command line.

EDIT . Based on comments, usage patterns are required here.

First you need to edit the .pro file to enable qtestlib:

 CONFIG += qtestlib 

Secondly, you need to include the necessary files:

  • For the QTest namespace (which includes qSleep ): #include <QTest>
  • For all elements in the QtTest module: #include <QtTest> . This is functionally equivalent to adding an include for each element that exists in the namespace.
+6
Sep 20 '10 at 15:58
source share

To use the standard sleep function, add the following to your .cpp file:

 #include <unistd.h> 

As in Qt version 4.8, the following sleep functions are available:

 void QThread::msleep(unsigned long msecs) void QThread::sleep(unsigned long secs) void QThread::usleep(unsigned long usecs) 

To use them, simply add the following to your .cpp file:

 #include <QThread> 

Link: QThread (via Qt documentation): http://doc.qt.io/qt-4.8/qthread.html

Otherwise, follow these steps ...

Modify the project file as follows:

 CONFIG += qtestlib 

Please note that in newer versions of Qt you will receive the following error:

 Project WARNING: CONFIG+=qtestlib is deprecated. Use QT+=testlib instead. 

... therefore, modify the project file as follows:

 QT += testlib 

Then, in your .cpp file, be sure to add the following:

 #include <QtTest> 

And then use one of the sleep functions like this:

 usleep(100); 
+6
Aug 16 '16 at 21:00
source share

Over the years, I have had many problems trying to get QApplication :: processEvents to work, as used in some of the best answers. IIRC, if several addresses eventually caused it, this could lead to some signals not being processed ( https://doc.qt.io/archives/qq/qq27-responsive-guis.html ). My usual preferred option is to use QEventLoop ( https://doc.qt.io/archives/qq/qq27-responsive-guis.html#waitinginalocaleventloop ).

 inline void delay(int millisecondsWait) { QEventLoop loop; QTimer t; t.connect(&t, &QTimer::timeout, &loop, &QEventLoop::quit); t.start(millisecondsWait); loop.exec(); } 
+4
Mar 24 '17 at 15:18
source share

Like some answers here, but maybe a little easier

 void MyClass::sleepFor(qint64 milliseconds){ qint64 timeToExitFunction = QDateTime::currentMSecsSinceEpoch()+milliseconds; while(timeToExitFunction>QDateTime::currentMSecsSinceEpoch()){ QApplication::processEvents(QEventLoop::AllEvents, 100); } } 
+2
Apr 23 '14 at 10:25
source share

If you want to use the cross-platform method for this, the general template must be obtained from QThread and create a function (static, if you want) in the derived class that will call one of the wait functions in QThread.

+1
Sep 20 '10 at 23:55
source share

The answer @ kshark27 for some reason didn’t work for me (because I'm using Qt 5.7?), So I ended up doing this:

 while (someCondition) { // do something QApplication::processEvents(); QThread::sleep(1); // 1 second }; 

If this is done in a GUI thread, it obviously introduces a 1 second delay before responding to user events. But if you can come to terms with this, this solution is probably the easiest to implement, and even Qt endorses it in the article β€œ Basics of threads” (see the section β€œ When to use alternatives to threads ”).

+1
Aug 31 '16 at 15:01
source share

we can use the following QT_Delay method:

 QTimer::singleShot(2000,this,SLOT(print_lcd())); 

This will wait 2 seconds before calling print_lcd() .

0
May 17 '19 at 7:41
source share



All Articles