Important: Please read the Qt Code of Conduct -

[SOLVED]QThread, the right way and sleep()

  • Hi!

    This time when I was using threads I've done it "the right way" ( creating object, connecting it with signals with its thread and moving it to thread).

    But now I've a slight problem. I need QThread()::msleep(). Which I cannot access through signals and slots nor directly by calling msleep() because is protected.

    Any proposal how can I solve this? or should I inherit QThread and forget about "done right"?

    I was thinking of deriving from QThread an creating slot for delay, or deriving QThread and placing everything in thread as I already did once in another program.

    How is this usually done ( the correct way)?


  • Here is how I did for Sleep:

    If your class is derived from QThread, you can add a static public method like this:
    static void Sleep(unsigned long ms) { QThread::msleep(ms); }
    Then you can call MyThread::Sleep(1000) to sleep one second. If your clss is not a subclass of QThread and you want it to have a Sleep method, you can add a QThread derived class embedded in your class which has a static method Sleep as above. Then you define a public static method Sleep in your class that deligates to Sleep method of embedded class:
    void MyClass::Sleep( unsigned long ms)
    Hope it works for you.

    [Edit: Added @ tags around code -- mlong]

  • I did as you did in your first solution.
    I derived QThread and reimplemented all functions for sleep (static), so that I can use them in my worker class.


    Marked as solved.


  • Moderators

    Just for your interest, the sleep functions are public in Qt 5

Log in to reply