开发者

Qt :what is the best way to implement sleep function inside QThread?

开发者 https://www.devze.com 2023-03-11 09:55 出处:网络
i invoking QThread with cr开发者_高级运维eating object and using MoveToThread function like it suggest

i invoking QThread with cr开发者_高级运维eating object and using MoveToThread function like it suggest inside the Object i have loop and i need to be able to set sleep for few seconds between iterations ( to update the main GUI ) searching the web got me to this link:

http://www.qtcentre.org/threads/476-where-s-the-sleep%28%29-func

but this not working inside threads , what is the best way to do this ?


Have a look at

void    msleep ( unsigned long msecs )
void    sleep ( unsigned long secs )
void    usleep ( unsigned long usecs )

methods of QThread

These methods are all protected in qt4. So you need to derive from QThread to access them if you are using qt4. I am not sure if they were protected in qt3 or not.


This is how I got around the problem of the QThread sleep functions being protected:

// QThread has static sleep functions; but they're protected (duh).
// So we provide wrapper functions:
//
//    void MyLib::sleep (unsigned long secs)
//    void MyLib::msleep (unsigned long msecs)
//    void MyLib::usleep (unsigned long usecs)

#include <QThread>

namespace MyLib
  {
  class DerivedFromQThread : protected QThread
    {
  public:
    static void sleep (unsigned long secs)   { QThread::sleep (secs) ; }
    static void msleep (unsigned long msecs) { QThread::msleep (msecs) ; }
    static void usleep (unsigned long usecs) { QThread::usleep (usecs) ; }
    } ;
  void sleep (unsigned long secs)   { DerivedFromQThread::sleep (secs) ; }
  void msleep (unsigned long msecs) { DerivedFromQThread::msleep (msecs) ; }
  void usleep (unsigned long usecs) { DerivedFromQThread::usleep (usecs) ; }
  } // namespace MyLib
0

精彩评论

暂无评论...
验证码 换一张
取 消