我試圖啓動一個工作線程的事件循環定時器,但我得到這個錯誤: QObject::startTimer: Timers can only be used with threads started with QThread
的QObject :: startTimer所:定時器只能與線程使用開始的QThread
請告訴我問題呢?
#include <QObject>
#include <QThread>
#include <QTimer>
class A : public QObject
{
Q_OBJECT
public:
A();
private:
QThread m_workerThread;
QTimer m_myTimer;
};
A::A()
{
this->moveToThread(&m_workerThread);
m_myTimer.moveToThread(&m_workerThread);
m_workerThread.start();
m_myTimer.start(1000);
}
爲什麼定時器移動到胎面呢? –
@SebastianLange計時器應該位於大部分信號接收者所在的相同線程中。否則,當發射器線程停止時,您將停止收件人線程。 –
是真的。在我的腦海裏出現了一些問題,仍然是單槍匹馬。但儘管如此,如果整個對象位於線程,我不會需要移動計時器居住在此對象給其他線程太... –