Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sleep in a QRunnable?

Tags:

c++

qt

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

like image 904
CDT Avatar asked Jun 04 '13 02:06

CDT


2 Answers

  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.

like image 144
Lol4t0 Avatar answered Nov 05 '22 00:11

Lol4t0


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.

like image 3
totymedli Avatar answered Nov 04 '22 23:11

totymedli