头文件中
private:
QTimer *m_pTimer;
QThread *m_pThread;
private slots:
void timeOutSlot();
.cpp中
void ControlWidget::startThreadTime() {
qDebug() << "howard ===startThreadTime";
m_pThread = new QThread(this);
m_pTimer = new QTimer();
m_pTimer->moveToThread(m_pThread);
m_pTimer->setTimerType(Qt::PreciseTimer);
//temp_refresh_time是时间间隔
m_pTimer->setInterval(temp_refresh_time);
connect(m_pThread, SIGNAL(started()), m_pTimer, SLOT(start()));
connect(m_pTimer, SIGNAL(timeout()), this, SLOT(timeOutSlot()), Qt::DirectConnection);
m_pThread->start();
}
void ControlWidget::timeOutSlot(){
//定时时间到的回调
}
如果再次启动的话
m_pTimer->stop();
m_pTimer->destroyed();
m_pThread->wait();
m_pThread = new QThread(this);
m_pTimer = new QTimer();
m_pTimer->moveToThread(m_pThread);
m_pTimer->setTimerType(Qt::PreciseTimer);
m_pTimer->setInterval(temp_refresh_time);
connect(m_pThread, SIGNAL(started()), m_pTimer, SLOT(start()));
connect(m_pTimer, SIGNAL(timeout()), this, SLOT(timeOutSlot()), Qt::DirectConnection);
m_pThread->start();