How to make the QT program use sleep

Source: Internet
Author: User
Tags sleep function qtest

Why does QT not provide sleep?

Someone asked me from time to time on the Forum:

  1. Why does QT not provide cross-platform sleep functions?
  2. Why didn't the interface respond after using platform-related sleep or nanosleep?
  3. Qthread provides the sleep function with the protected permission. How do I use it in the main thread?
  4. How can I hide the console in Windows by using qsleep in qtest?

These problems actually come down to one point: using these functions in the main thread is a kind of error, which directly causes the interface to fail to be refreshed and the user and program cannot interact.

QT is not provided because you do not need to use the sleep function in the main thread.

How to let the program wait for a period of time qtime


QTime t;t.start();while(t.elapsed()<1000);

This type of endless loop is also a common error. But it is relatively simple to change it to the correct one:


QTime t;t.start();while(t.elapsed()<1000)    QCoreApplication::processEvents();

The program keeps responding to events.


This is a new class introduced by qt4.7. Compared with qtime, it provides a faster method for calculating elapsed time.

QElapsedTimer t;t.start();while(t.elapsed()<1000)    QCoreApplication::processEvents();
Qtest: qwait

This is the waiting function provided by the qtest module.

Below is the source code (which is similar to the code above ?) :

namespace QTest{    inline static void qWait(int ms)    {        Q_ASSERT(QCoreApplication::instance());        QElapsedTimer timer;        timer.start();        do {            QCoreApplication::processEvents(QEventLoop::AllEvents, ms);            QTest::qSleep(10);        } while (timer.elapsed() < ms);    }...

Actually, there is no magic, right? But because of its qtest module, we should not use it in the program.


Using Partial eventloop with qtimer is also a good choice. Example:

    QEventLoop eventloop;    QTimer::singleShot(100, &eventloop, SLOT(quit()));    eventloop.exec();
Qtimer and qbasictimer

These two are not directly related to this article, and qtimer is probably familiar to everyone. Qbasictimer is estimated to be rarely used.

  • Compared with qtimer, qbasictimer is faster, lightweight, and underlying.
  • Compared with qtimer, it is not a derived class of qobject.
Cross-platform sleep

Although we mentioned it at the beginning, we don't need it. But it is not ruled out that you actually need this thing in a certain situation. How to implement a cross-platform sleep?

We also mentioned at the beginning that both the qthread class and the qtest module provide sleep functions. In fact, we only need to look at their source code:

The function in the qtest module is very simple (sleep is called in windows, and nanosleep is called in other platforms ):

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}

Looking at the source code of qthread, sleep is also directly called in windows, but non-Windows implementation is much more complicated than this:


/*/Internal <br/> helper function to do thread sleeps, since usleep ()/nanosleep () <br/> aren't reliable enough (in terms of behavior and availability) <br/> */<br/> static void thread_sleep (struct timespec * ti) <br/>{< br/> pthread_mutex_t CTX; <br/> pthread_cond_t CND; <br/> pthread_mutex_init (& CTX, 0); <br/> pthread_cond_init (& CND, 0); <br/> pthread_mutex_lock (& CTX ); <br/> (void) pthread_cond_timedwait (& CND, & CTX, Ti); <br/> pthread_mutex_unlock (& CTX); <br/> pthread_cond_destroy (& CND ); <br/> pthread_mutex_destroy (& CTX); <br/>}< br/> void qthread: Sleep (unsigned long SECs) <br/>{< br/> struct timeval TV; <br/> gettimeofday (& TV, 0); <br/> struct timespec Ti; <br/> Ti. TV _sec = TV. TV _sec + secs; <br/> Ti. TV _nsec = (TV. TV _usec * 1000); <br/> thread_sleep (& Ti); <br/>}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.