CDT
CDT

Reputation: 10621

How to sleep in a QRunnable?

It seems QRunnable does not have a sleep method.
How can I call method like QThread::sleep in a QRunnable ?

Upvotes: 7

Views: 2484

Answers (2)

Lol4t0
Lol4t0

Reputation: 12547

  1. Don't use platform specific functions. The great advantage of Qt is that it is pretty easy portable. Don't ruin it just with sleep

  2. You, can use QThread::sleep from QRunnable or QtConcurent only in Qt 5, as it is declared public there:

void QThread::sleep ( unsigned long secs ) [static protected] // Qt 4.8

void QThread::sleep(unsigned long secs) [static] // Qt 5.0

You can use mutex as workaround for earlier Qt versions:

QMutex m(QMutex::NonRecursive);
m.lock();
m.tryLock(timeout);

Mutex will fail to lock recursively and will wait for timeout.

Upvotes: 9

totymedli
totymedli

Reputation: 31058

Just simply use ::sleep() it isn't in Qt it's a POSIX.1-2001 function.

Also you can try this code, because QThread::sleep() calls ::Sleep()

class mythreadhelper : public QThread
{
   public:
   static void mysleep(int ms)
   {
      return sleep(ms);
   }
};

This question was answered at Qt Centre with a response from Nokia Certified Qt Developer.

Upvotes: 3

Related Questions