Results 1 to 16 of 16

Thread: QObject with QTimer moved to QThread - QTimer::timeout() not processed until wait()

  1. #1
    Join Date
    Apr 2012
    Posts
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default QObject with QTimer moved to QThread - QTimer::timeout() not processed until wait()

    Hi guys!
    I have the following problem:
    1. I want get thread doing some work and update the label in the main thread every second.
    2. I got it working subclassing QThread bu according to some sources it is not a good way.
    3. I decided to subclass a QObject (MyObject) with internal QTimer* _myTimer, and doWork() function with an internal loop, This object is after construction moved to newly created QThread* _thread.

    My problem is that _myTimer's timeout() signal which is connected to tick() slot of the _myObject, is not executed after _thread->start(). tick() is executed only after doing _thread->wait(). Both the tick() slot and myTimer exist in the myThread so i do not know what is the problem. It seems like there is no event loop in myThread.

    Here is my code:
    Qt Code:
    1. class MyObject : QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. MyObject(QObject *parent=0);
    6.  
    7. public slots:
    8. void tick();
    9. void doWork();
    10.  
    11. private:
    12. QTimer* _myTimer;
    13. };
    14.  
    15. void MyObject::doWork()
    16. {
    17. _myTimer = new QTimer(this);
    18. _myTimer->setInterval(1000);
    19. connect(_myTimer, SIGNAL(timeout()), this, SLOT(tick())/*,Qt::DirectConnection*/);
    20. _myTimer->start()
    21. qDebug() << QThread::currentThreadId() << "doWork()";
    22. while(..)
    23. {
    24. ...
    25. }
    26. }
    27.  
    28. void MyObject::tick()
    29. {
    30. emit tickSignalToGUI(..);
    31. qDebug() << QThread::currentThreadId() << "tick()";
    32. }
    33.  
    34. // and somewhere in GUI thread
    35. (...)
    36. _thread = new QThread(this);
    37. _myObject = new MyObject(); /
    38. _myObject->moveToThread(_thread);
    39. _thread->start();
    40. // _myObject->doWork(); //this blocks execution
    41. QMetaObject::invokeMethod(_myObject,"doWork");
    42. qDebug() << QThread::currentThreadId() << "GUI thread";
    43. (...)
    To copy to clipboard, switch view to plain text mode 

    Any ideas what is wrong with this approach?

    best regards,
    Last edited by airproject; 4th August 2012 at 15:09.

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QObject with QTimer moved to QThread - QTimer::timeout() not processed until wait

    what you are doing is even worse than subclassing QThread (not that subclassing QThread is bad)


    You are blocking the main thread with the while(...) loop in MyObject.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Apr 2012
    Posts
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QObject with QTimer moved to QThread - QTimer::timeout() not processed until wait

    Quote Originally Posted by amleto View Post
    what you are doing is even worse than subclassing QThread (not that subclassing QThread is bad)


    You are blocking the main thread with the while(...) loop in MyObject.
    Are you sure? As myObject with its while loop is moved to another thread it seems to run concurently to main thread. At least the GUI stays responsive and processes all the events.

    best regads.

  4. #4
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QObject with QTimer moved to QThread - QTimer::timeout() not processed until wait

    well, now that you have changed it to QMetaObject::invokeMethod with your edit it should be a lot better...

    Now you're just blocking the other thread with that while(...)


    You know it would be handy if you didn't hide code that is directly relevant to your question.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  5. #5
    Join Date
    Apr 2012
    Posts
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QObject with QTimer moved to QThread - QTimer::timeout() not processed until wait

    sorry for that.. your footnote really make sense
    Do you have any idea why the signals from timer are not processed during the doWork()? They start to be processed after i try to finish _myThread with _mythread->wait(). :/

    I cant find any good way to let the timer work in a separate thread.

  6. #6
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QObject with QTimer moved to QThread - QTimer::timeout() not processed until wait

    how many times do you think I shall say that your while(...) is* blocking before you will think that might be the cause?

    *guessing since you are still hiding that code...
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  7. #7
    Join Date
    Apr 2012
    Posts
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QObject with QTimer moved to QThread - QTimer::timeout() not processed until wait

    One with some additional comment would be enough.
    So how to avoid this devilish while(..) and still do some processing loop that can coexist with the timer in harmony?

  8. #8
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QObject with QTimer moved to QThread - QTimer::timeout() not processed until wait

    use signals and slots. not while(true) or equivalent
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  9. #9
    Join Date
    Apr 2012
    Posts
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QObject with QTimer moved to QThread - QTimer::timeout() not processed until wait

    My doWork() is processing some number of items. It should process this data unless user presses Pause button or Stop button. I initially made a QThread subclass which in run() had while(true) (i guess that this is not a bad practice in thread?) and some exit conditions checking flags set by signals from GUI. As "this->moveToThread(this)" trick is not a good thing to do i have rebuild my QThread subclass to QObject subclass.
    How would you rebuild the while(true){} processing loop to fit "QObject moved to QThread" approach?
    Best regards.

  10. #10
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QObject with QTimer moved to QThread - QTimer::timeout() not processed until wait

    How do you expect me to say how you can refactor your while loop when you have given no information about what it actually does and where the input comes from

    I give up. Just use this.
    http://doc.qt.io/qt-4.8/qcoreapplication.html#processEvents
    Last edited by amleto; 4th August 2012 at 16:45.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  11. #11
    Join Date
    Apr 2012
    Posts
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QObject with QTimer moved to QThread - QTimer::timeout() not processed until wait

    If you suggest to use:
    Qt Code:
    1. doWork()
    2. {
    3. (...)
    4. while(true)
    5. {
    6. QCoreApplication::processEvents();
    7. (...)
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 
    it does not solve the problem.

  12. #12
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QObject with QTimer moved to QThread - QTimer::timeout() not processed until wait

    read my sig


    All your timer does is send signal. you can put your timer anywhere. Why does it have to be in heavy processing thread?
    Last edited by amleto; 4th August 2012 at 16:57.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  13. #13
    Join Date
    Apr 2012
    Posts
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QObject with QTimer moved to QThread - QTimer::timeout() not processed until wait

    I can redesign and put a timer outside the processing thread. Still for my own knowledge i would like to know it there is a way of installing the QTimer inside a Thread.

    Sorry for not pasting the original code. It is not my property and i cannot share it. As it is quite big i just tried to extract the highlights.

  14. #14
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QObject with QTimer moved to QThread - QTimer::timeout() not processed until wait

    You can put a timer anywhere you want, including in threads. But if you block the event loop of that thread then signals wont fly out and slots inside wont be executed. It's pretty straight forward.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  15. #15
    Join Date
    Apr 2012
    Posts
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QObject with QTimer moved to QThread - QTimer::timeout() not processed until wait

    I agree with you. I do not know how to make these timer events propagate in my while() loop. According to docs: QCoreApplication:rocessEvents()
    "Processes all pending events for the calling thread according to the specified flags until there are no more events to process."

    So putting it inside a while() loop should do the job - sadly it does not, ..or i am doing something wrong.
    Thank you anyway.

  16. #16
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QObject with QTimer moved to QThread - QTimer::timeout() not processed until wait

    you could be doing any number of wrong things so I'm not going to speculate any further without code.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. QTimer in QThread doesn't start or timeout
    By Boron in forum Qt Programming
    Replies: 9
    Last Post: 21st October 2011, 14:51
  2. Get QTimer's seconds till next timeout?
    By hakermania in forum Newbie
    Replies: 12
    Last Post: 13th February 2011, 00:49
  3. QTimer delayed timeout
    By DavidY in forum Qt Programming
    Replies: 4
    Last Post: 10th December 2009, 22:34
  4. Replies: 2
    Last Post: 9th September 2009, 01:26
  5. QThread/QObject and QTimer
    By swiety in forum Qt Programming
    Replies: 2
    Last Post: 25th January 2008, 09:37

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.