1

I'm creating a UI application for Raspberry PI to read data from sensor on definite timeout (5 seconds). Problem is the QTimer timeout slot is called for multiple times

{   //at system init
readTempCur = new QTimer(this);
connect(readTempCur, SIGNAL(timeout()), this, SLOT(readSensor()));
readTempCur->start(SAMPLINGTIME);
readSensor();   //added to call on boot itself, can be removed
}
void HomePage::readSensor(void) {
   readTempCur->stop();
   qDebug() << "Read Sensor triggerred at " <<QDateTime::currentDateTime().toString();
   //DO my actions
   readTempCur->start(SAMPLINGTIME);
 }

Screenshot for QTimer triggered multiple times

[edit for answer] The most probable case for such issue is conneccting the slot to the signal that already conneccted; this will trigger slot for 'n' number times it got connected, design should take care not to reconnect again.

4

2 回答 2

2

QTimer::start函数将启动/重新启动计时器。

您的readSensor函数会停止计时器,然后再次重新启动它。

删除start以修复它。

void HomePage::readSensor(void) {
   readTempCur->stop();
   qDebug() << "Read Sensor triggerred at " <<QDateTime::currentDateTime().toString();
   //DO my actions
   //readTempCur->start(SAMPLINGTIME);
 }

PS如果你想运行一次计时器你可以使用singleShoot

QTimer::singleShot(SAMPLINGTIME, this, SLOT(readSensor()));
于 2019-05-05T14:12:52.300 回答
0

不要停止或重新启动计时器readSensor()。做就是了:

void HomePage::readSensor(void)
{
    qDebug() << "Read Sensor triggerred at " <<QDateTime::currentDateTime().toString();
    //DO my actions
}

另外,请确保SAMPLINGTIME以毫秒为单位。5秒,SAMPLINGTIME应该是5000。

于 2019-05-05T14:28:07.853 回答