Results 1 to 16 of 16

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    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.

  3. #3
    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 15: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.

  4. #4
    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.

  5. #5
    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 15: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.

  6. #6
    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.

  7. #7
    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.

  8. #8
    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.

  9. #9
    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, 13:51
  2. Get QTimer's seconds till next timeout?
    By hakermania in forum Newbie
    Replies: 12
    Last Post: 12th February 2011, 23:49
  3. QTimer delayed timeout
    By DavidY in forum Qt Programming
    Replies: 4
    Last Post: 10th December 2009, 21:34
  4. Replies: 2
    Last Post: 9th September 2009, 00:26
  5. QThread/QObject and QTimer
    By swiety in forum Qt Programming
    Replies: 2
    Last Post: 25th January 2008, 08: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.