Search code examples
qtsleepthread-sleep

Is there a function or method to suspend the main thread for a specified amount of time?


Is there a wrapper function for Sleep / usleep in Qt that can be used in the main thread, or do I have to write my own wrapper? It's not a lot of code, but would be a shame to write my own if there already is one.


Solution

  • Variant 1:

    Pro-file:

    CONFIG += qtestlib
    

    Code:

    #include <QTest>
    
    QTest::qSleep( 10000 ); // 10 sec
    

    Variant 2:

    #ifndef XSLEEP_H 
    #define XSLEEP_H 
    
    #include <QThread> 
    
    class Xsleep : public QThread 
    { 
    public: 
        static void msleep(int ms) 
        { 
            QThread::msleep(ms); 
        } 
    }; 
    
    #endif  
    
    C++:
    while (true) 
    { 
            ... 
            Xsleep::msleep(1000); // 1 sek 
            ... 
    
    }