How do I create a pause/wait function using Qt?

后端 未结 12 2097
执笔经年
执笔经年 2020-11-27 15:19

I\'m playing around with Qt, and I want to create a simple pause between two commands. However it won\'t seem to let me use Sleep(int mili);, and I can\'t find

相关标签:
12条回答
  • 2020-11-27 15:57

    If you want a cross-platform method of doing this, the general pattern is to derive from QThread and create a function (static, if you'd like) in your derived class that will call one of the sleep functions in QThread.

    0 讨论(0)
  • 2020-11-27 16:02

    Similar to some answers here, but maybe a little more lightweight

    void MyClass::sleepFor(qint64 milliseconds){
        qint64 timeToExitFunction = QDateTime::currentMSecsSinceEpoch()+milliseconds;
        while(timeToExitFunction>QDateTime::currentMSecsSinceEpoch()){
            QApplication::processEvents(QEventLoop::AllEvents, 100);
        }
    }
    
    0 讨论(0)
  • 2020-11-27 16:03

    Small +1 to kshark27's 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 );
        }
    }
    
    0 讨论(0)
  • 2020-11-27 16:06

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

    #include <unistd.h>
    

    As of 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 in your .cpp file:

    #include <QThread>
    

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

    Otherwise, perform these steps...

    Modify the project file as follows:

    CONFIG += qtestlib
    

    Note that in newer versions of Qt you will get the following error:

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

    ... so, instead 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 so:

    usleep(100);
    
    0 讨论(0)
  • 2020-11-27 16:08

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

    I would advise you to use this code rather than the sleep function as it won't let your GUI freeze.

    Here is the code:

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

    To delay an event by n seconds - use addSecs(n).

    0 讨论(0)
  • 2020-11-27 16:10

    we can use following method QT_Delay:

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

    This will wait for 2 seconds before calling print_lcd().

    0 讨论(0)
提交回复
热议问题