qt 要开启多个定时器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了qt 要开启多个定时器相关的知识,希望对你有一定的参考价值。
参考技术A//背景时12个帕尔贴 不要12个温度去曲线,每一个类似下图:
该类在线程启动12定时器计时,并停止,第13个定时器 定时读取温度
//头文件
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];
;
//源文件
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 "<<QThread::currentThreadId()<<"\\n";
killTimer( timerID );
//获取温度
void ThreadTimer::GetTem()
真是奇怪,js可以,jq却开启了多个定时器,谁能解答一下
timer = setInterval(mar,30);
//鼠标移上去清除时钟
box.onmouseover = function(event){
clearInterval(timer);
event.stopPropagation();
//这里是阻止冒泡,不加要冒泡到body上面,鼠标在body上面就会clearInterval(timer);
box.onmouseout = function(event){
timer = setInterval(mar, 30);
event.stopPropagation();
}
};
//用jq方式写
/*$(box).on(‘mouseover‘,function(){
clearInterval(timer);
})*/
// $(box).mouseover(function(event){
// clearInterval(timer);
// event.stopPropagation();
// $(box).mouseout(function(e){
// timer = setInterval(mar,30);
// e.stopPropagation();
// })
// });
以上是关于qt 要开启多个定时器的主要内容,如果未能解决你的问题,请参考以下文章
Qt中多个定时器共享一个槽函数,怎么确定是哪个定时器超时呢?