0

I am communicating with a remote device in qt, and sometimes the remote device is not answering. Therefore I implemented a timer which should notify the function readData() which is usually called when the device answers:

int timerID;

void stepperMworker::start_timer()
{
    this->timerID = startTimer(TIME_OUT);
}

void stepperMworker::readData(QString data)
{
    killTimer(this->timerID);
    //Process data
}

void stepperMworker::timerEvent(QTimerEvent *event)
{
    killTimer(this->timerID);
    this->readData(QString::number(-1));
}

The idea was that either readData() is called first from the receiving signal from external, and then stops the timer, or the timer itself signalizes the readData()-function after TIME_OUT milliseconds that the external device is not responding. Nevertheless sometimes I get the error Error: timer id 2 is not valid for object 0x98f270 (stepperMworker, ), timer has not been killed. How can I determine if the current timerID is not valid?

arc_lupus
  • 3,942
  • 5
  • 45
  • 81

1 Answers1

0

This looks like you kill the timer twice. Do you prevent that readData is called after you get a timerEvent? If you only want to suppress the warning you can insert this->timerID = 0 after the killTimer call (see the function void QObject::killTimer(int id) in file /qtbase/src/corelib/kernel/qobject.cpp.

hmuelner
  • 8,093
  • 1
  • 28
  • 39
  • I can only guess that readData is not called after the timerEvent, because the incoming data will only take approximately 0.5 sec, but the timer takes 5 sec. I don't prevent it in other ways. – arc_lupus Dec 11 '15 at 16:55