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.

Kanni1303
  • 73
  • 11
  • Do you want a single-shot for the timer? in your `readSensor` you called the `start` function, this why it will call it again. – Simon May 05 '19 at 14:05
  • yes I added this to solve whether my slot functions takes execution more than timeout period, On beginning I stopped the timer, and after finishing all the jobs, I start the timer again. – Kanni1303 May 05 '19 at 14:12
  • So use one timer for sensor reading and another for execution timeout – Simon May 05 '19 at 14:14
  • "*Problem is the QTimer timeout slot is called for multiple times*" Yes, of course it is. That's how QTimer works by default. But it is not clear what it is you actually want. Do you only want it to trigger once? – Nikos C. May 05 '19 at 14:21
  • Here multiple times at same time not on defined timeout, can you see the screenshot, at exactly same time stamp two times it got triggered, ans this is random behavior – Kanni1303 May 05 '19 at 14:24
  • Thanks guys got help from QT people, the sysInit() is called for multiple times which reconnected same slot to same signal once again. I hope fixing that will probably fix this one – Kanni1303 May 05 '19 at 16:05
  • If you are calling `sysInit` multiple times, then you are creating a new timer each time, so you would have multiple timers running. – thuga May 06 '19 at 09:57

2 Answers2

2

The QTimer::start function will start/restart the timer.

Your readSensor function stops the timer and then restart it again.

remove the start to fix it.

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

P.S. If you want to run the timer once you can use the singleShoot

QTimer::singleShot(SAMPLINGTIME, this, SLOT(readSensor()));
Simon
  • 1,522
  • 2
  • 12
  • 24
  • sorry this won't work, on beginning, I stopped the timer, even removing the start() and stop() inside the slot function same behavior, This occurs not on all the call, but randomly, sometimes it is called for 2 times, sometimes it is called for even 4-5 times – Kanni1303 May 05 '19 at 14:15
  • did you try to remove just the `start()`? what is the `SAMPLINGTIME` value? – Simon May 05 '19 at 14:17
  • I think you will have to provide more code. I can't see the problem here. please update your question [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Simon May 05 '19 at 14:24
  • I also doubt the same, problem should be some where else in the project, but I need some suggestions whether this multiple timeout trigger is possible or not, or it is a bug from Qt12.0 – Kanni1303 May 05 '19 at 14:54
0

Do not stop nor restart the timer in readSensor(). Just do:

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

Also, make sure SAMPLINGTIME is given in milliseconds. For 5 seconds, SAMPLINGTIME should be 5000.

Nikos C.
  • 50,738
  • 9
  • 71
  • 96
  • Tried this, but result is same. – Kanni1303 May 05 '19 at 14:35
  • @Kanni1303 Then I'm afraid nobody can help you. The problem lies elsewhere and is not in the code you posted. – Nikos C. May 05 '19 at 14:53
  • Thanks guys! got help from QT people, the sysInit() is called for multiple times on random; which reconnected same slot to same signal once again. I hope fixing that will probably fix this one – Kanni1303 May 05 '19 at 16:05