5YrsLaterDBA
5YrsLaterDBA

Reputation: 34760

how to sleep in Qt4

I just found it is NOT easy to make a sleep call in Qt4. I wrote some code in Qt5 and have few QThread::msleep() calls in my main() and other places. I want to convert those code to Qt4 but cannot find an easy way to convert these msleep calls.

error: C2248: 'QThread::msleep' : cannot access protected member declared in class 'QThread'

Somebody suggested subclass the QThread class. Why I need to go that far? no simple way to just sleep a while?

Upvotes: 2

Views: 3192

Answers (2)

Marek R
Marek R

Reputation: 37697

You can subclass QThread to expose this methods if you insist on sleep:

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

Upvotes: 4

asandroq
asandroq

Reputation: 575

You can use a wait condition:

QWaitCondition wc;
QMutex mutex;
QMutexLocker locker(&mutex);
wc.wait(&mutex, milliseconds);

Upvotes: 6

Related Questions