QT Custom Sleep delay function (cleverly uses the time difference, but this seems to be the CPU is full of cells, rather than sleeping)

Source: Internet
Author: User
Tags sleep function



QT does not, like VC + + WIN32/MFC programming, provide a ready-made sleep function to call. Qt encapsulates the sleep function in the Qthread class. A child thread can call the sleep function. But what if the user wants to implement the delay function in the main thread? method is to customize the sleep delay function. The time difference is realized by Qdatetime.


#include <QDateTime>

void MainWindow :: sleep (int msec) // Custom Qt delay function, unit millisecond
{
     QDateTime last = QDateTime :: currentDateTime ();
     QDateTime now;
     while (1)
     {
         now = QDateTime :: currentDateTime ();
         if (last.msecsTo (now)> = msec)
         {
             break;
         }
     }
} 


http://blog.csdn.net/libaineu2004/article/details/39161697



QT Custom Sleep delay function (cleverly uses the time difference, but this seems to be the CPU is full of cells, rather than sleeping)


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: info-contact@alibabacloud.com 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.