Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give a delay in loop execution using Qt

Tags:

loops

qt

In my application I want that when a loop is being executed, each time the control transfers to the loop, each execution must be delayed by a particular time. How can I do this?

like image 673
CuriousCase Avatar asked Sep 30 '10 14:09

CuriousCase


People also ask

How do you use a QT timer?

The QTimer class provides a high-level programming interface for timers. To use it, create a QTimer, connect its timeout() signal to the appropriate slots, and call start(). From then on, it will emit the timeout() signal at constant intervals.

How do you create a thread in Qt?

The start() method creates a new thread and calls the reimplemented run() method in this new thread. Right after start() is called, two program counters walk through the program code. The main function starts with only the GUI thread running and it should terminate with only the GUI thread running.


1 Answers

EDIT (removed wrong solution). EDIT (to add this other option):

Another way to use it would be subclass QThread since it has protected *sleep methods.

QThread::usleep(unsigned long microseconds); QThread::msleep(unsigned long milliseconds); QThread::sleep(unsigned long second); 

Here's the code to create your own *sleep method.

#include <QThread>      class Sleeper : public QThread { public:     static void usleep(unsigned long usecs){QThread::usleep(usecs);}     static void msleep(unsigned long msecs){QThread::msleep(msecs);}     static void sleep(unsigned long secs){QThread::sleep(secs);} }; 

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. If the underlying operating system timers support the resolution.

like image 82
Live Avatar answered Oct 03 '22 20:10

Live