Results 1 to 7 of 7

Thread: QTimer in a worker thread

  1. #1
    Join Date
    Aug 2011
    Posts
    25
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTimer in a worker thread

    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?

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,477
    Thanked 274 Times in 269 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTimer in a worker thread

    You can start timer in any thread but in Flex_thread it will be not working because You don't start event loop in method Flex_thread::run.
    Look at the source of QThread::run.

  3. #3
    Join Date
    Aug 2011
    Posts
    25
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTimer in a worker thread

    I want to start a QTimer::singleShot in line 28 in FlexThread.
    I do not manage to start it. I have tried to put in following line:
    QTimer::singleShot(1000, this, SLOT(updateCounter()));

    while updateCounter() set the counterBytes to 0.

    I have also put exec() between line 14 and 15 in Flex_thread. But I think the timer will not start. The updateCounter() will not start.

    How may I do this?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTimer in a worker thread

    Quote Originally Posted by inger View Post
    I have also put exec() between line 14 and 15 in Flex_thread.
    Please think for a short while when this line is going to be executed.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Aug 2011
    Posts
    25
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTimer in a worker thread

    Oh sorry,
    that will never be executed.

    Are you able to help me how to get the QTimer started?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTimer in a worker thread

    Read this: [wiki]Keeping the GUI Responsive[/wiki]
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    inger (7th February 2012)

  8. #7
    Join Date
    Nov 2012
    Posts
    1
    Qt products
    Platforms
    Maemo/MeeGo

    Default Re: QTimer in a worker thread

    Quote Originally Posted by inger View Post
    I want to start a QTimer::singleShot in line 28 in FlexThread.
    I do not manage to start it. I have tried to put in following line:
    QTimer::singleShot(1000, this, SLOT(updateCounter()));

    while updateCounter() set the counterBytes to 0.

    I have also put exec() between line 14 and 15 in Flex_thread. But I think the timer will not start. The updateCounter() will not start.

    How may I do this?

    Thanks for the post... I really like the answer..

Similar Threads

  1. QAxObject worker thread
    By semajnosnibor in forum Qt Programming
    Replies: 2
    Last Post: 21st January 2012, 16:10
  2. Replies: 3
    Last Post: 20th September 2011, 20:13
  3. Worker thread
    By doggrant in forum Newbie
    Replies: 4
    Last Post: 3rd November 2009, 15:49
  4. Worker thread problem
    By hkvm in forum Qt Programming
    Replies: 4
    Last Post: 6th September 2009, 20:12
  5. Main thread - worker thread communication.
    By kikapu in forum Newbie
    Replies: 25
    Last Post: 23rd May 2007, 22:09

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.