qt 要开启多个定时器

//背景时12个帕尔贴 不要12个温度去曲线,每一个类似下图:

b27.jpg

该类在线程启动12定时器计时,并停止,第13个定时器 定时读取温度
//头文件

ifndef THREADTIMER_H

define THREADTIMER_H

include

define MAX_REAGENT (12) //PCR最多八连排数量

class QThread;
class QTimer;

class ThreadTimer : public QObject
{
Q_OBJECT
public:
ThreadTimer();
~ThreadTimer();
public slots:
//index八连排索引, sec是秒 开启定时器
void StartTimer(int index, int sec );
void StartGetTem( int timeout );
void StopGetTem( );
private:
void GetTem();
signals:
void TimeOut(int index);
private:
virtual void timerEvent(QTimerEvent event);
private:
QTimer * m_pTimer;
QThread
m_pCurrThread;
int m_nTimer[MAX_REAGENT];
};

endif // THREADTIMER_H

//源文件

include "threadtimer.h"

include

include

include

include

ThreadTimer::ThreadTimer()
{
memset(m_nTimer,0,sizeof(m_nTimer));
m_pCurrThread = new QThread;
m_pTimer = new QTimer(this);
connect(m_pTimer, &QTimer::timeout, this, &ThreadTimer::GetTem);
this->moveToThread(m_pCurrThread);
m_pCurrThread->start();
}

ThreadTimer::~ThreadTimer()
{
if( nullptr != m_pCurrThread)
{
m_pCurrThread->quit();
m_pCurrThread->wait();
m_pCurrThread->deleteLater();
}
}

void ThreadTimer::StartTimer(int index, int timeout )
{
m_nTimer[index] = startTimer( timeout* 1000);
}

void ThreadTimer::StartGetTem( int timeout )
{
m_pTimer->start( timeout*1000 );
}

void ThreadTimer::StopGetTem( )
{
m_pTimer->stop();
}

//计时任务
void ThreadTimer::timerEvent(QTimerEvent *event)
{
int timerID = event->timerId();
for( int i=0;i< MAX_REAGENT;i++)
{
if( timerID == m_nTimer[i])
{
m_nTimer[i] = -1;
emit TimeOut(i);
break;
}
}
qDebug()<<"ThreadTimer::timerEvent "< killTimer( timerID );
}

//获取温度
void ThreadTimer::GetTem()
{

}

你可能感兴趣的:(qt 要开启多个定时器)