问:在QThread中实现睡眠功能的最佳方法是什么?

时间:2011-06-07 12:34:53

标签: c++ qt sleep qthread

我通过创建对象调用QThread并使用它建议的MoveToThread函数 在对象内部我有循环,我需要能够在迭代之间设置睡眠几秒钟(更新主GUI)搜索网络让我到这个链接:
http://www.qtcentre.org/threads/476-where-s-the-sleep%28%29-func
但这不能在线程内部工作,最好的方法是什么?

2 个答案:

答案 0 :(得分:3)

看看

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

QThread

的方法

这些方法在qt4中都受到保护。因此,如果使用qt4,则需要从QThread派生来访问它们。我不确定他们是否在qt3中受到保护。

答案 1 :(得分:0)

这就是我解决QThread sleep被保护函数的问题:

// 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