Hi,

I have problems with starting a QTimer or singleshot in a worker thread. Is that true that the timers only will start in the main thread?

My main start an object startIntegrator, that is a QObject. This object start a new thread called FB_thread (QThread). The run() of this thread go to a function , called receiveMsg(), that check if something come to a defined com-port. I want to count these incomming bytes. I want to start a timer when the run()-function receive something. When this timer timeout(), I want to put the counter to zero.

I want to put the reading function in a thread because I will have several threads to read an put into several queues.
My communication procedure with the commPort is qextserialcommport.

In addition to that timer mentioned, I want to start a timer that go into FB_thread's .
I use Ubuntu OS.

My question is:
Where do I put the counter, and how?

I have tried several places in my program, but the Timer will not be set to zero.
My last try is like this:
Qt Code:
  1. startIntegrator::startIntegrator(QObject *parent) :
  2. QObject(parent)
  3. {
  4. FB_thread = new Flex_thread;
  5. FB_thread->start();
  6.  
  7. flexiTimer = new QTimer();
  8. flexiTimer->setInterval(5);
  9. flexiTimer->start();
  10.  
  11. connect(flexiTimer, SIGNAL(timeout()), FB_thread, SLOT(checkFlexiCommPort()));
  12. connect(flexiTimer, SIGNAL(timeout()), FB_thread, SLOT(flexiTimerSignal()));
  13. connect(FB_thread, SIGNAL(setCounterToZero()), this, SLOT(startReceiverTimer()));
  14. connect(this->updateCounterTimer, SIGNAL(timeout()), FB_thread, SLOT(updateCounter()));
  15. connect(flexiTimer, SIGNAL(timeout()), this, SLOT(startUpRoutine()));
  16. FBToFLTP->wait();
  17. FB_thread->wait();
  18. }
  19.  
  20. void startIntegrator::startReceiverTimer()
  21. {
  22. qDebug() << "startReceiverTimer()";
  23. updateCounterTimer = new QTimer();
  24. updateCounterTimer->start(0);
  25. }
  26.  
  27. void startIntegrator::startUpRoutine()
  28. {
  29. FB_thread->checkFlexiCommPort();
  30. }
To copy to clipboard, switch view to plain text mode 

And the Flex_thread is:
Qt Code:
  1. Flex_thread::Flex_thread(QObject *parent) :
  2. QThread(parent)
  3. {
  4. flexiCommPortNumber = "/dev/ttyUSB0";
  5. // some code...
  6. connect(this, SIGNAL(setCounterToZero()), this, SLOT(checkSignal()));
  7. }
  8.  
  9. void Flex_thread::run()
  10. {
  11. do
  12. {
  13. receiveMsg();
  14. }while(!stopped);
  15. }
  16.  
  17. void Flex_thread::checkSignal()
  18. {
  19. qDebug() << "Signal ok!!"; // Just for checking that this is used
  20. }
  21.  
  22. void Flex_thread::receiveMsg()
  23. {
  24. int numBytes = CommPort->bytesAvailable(); // Check if the data is there
  25. if(numBytes > 0)
  26. {
  27. emit this->setCounterToZero(); // When something comes to this part of the program, I want to reset counterBytes after 1 sec.
  28. // I have tried to initiate and start a counter here also
  29. if (numBytes > 20) numBytes = 20;
  30. char buff[20];
  31. CommPort->readData(buff, numBytes); // Read bytes from CommPort
  32.  
  33. counterBytes = counterBytes + numBytes;
  34.  
  35. // ... Here I take care of the incomming data
  36. }
  37. }
  38. void Flex_thread::updateCounter()
  39. {
  40. counterBytes = 0;
  41. }
  42. void Flex_thread::checkFlexiCommPort()
  43. {
  44. // do something
  45. }
To copy to clipboard, switch view to plain text mode 

Can someone help me with these timers?