[Qt-interest] Delay of code execution

Matthias Pospiech matthias.pospiech at gmx.de
Tue Apr 13 17:09:49 CEST 2010


I was using the following code:

--- delay.h ----
#include <QtCore/QTime>
inline void delay(long ms)
{
        QTime time;
        time.start();
        while (time.elapsed()<ms);       
}

then I found that the following should be possible as well:

--- delay.h ----
#include <QtCore/QThread>

class SleeperThread : public QThread
{
public:
    static void msleep(unsigned long msecs)
    {
        QThread::msleep(msecs);
    }
};

inline void delay(long ms)
{
       SleeperThread::msleep(ms);
}


If I however use the latter approach inside a thread I see that the 
function delay(1000) returns immediately.
Did I understand something wrong? Which is the recommanded solution?
I know that in a thread I could as well use msleep, but I would like to 
have a solution which can be used anywhere
in the programm and also outside of QThread classes.

Matthias



More information about the Qt-interest-old mailing list