Results 1 to 14 of 14

Thread: QTimer / QThread question

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2010
    Posts
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default QTimer / QThread question

    I have an application that sorts arrays of ints (6 at a time). There is a main QWidget that has 6 subclassed QWidgets that handle the drawing of the sort progress. There is also another thread that handles all the signals from buttons and sliders. This one creates the 6 sort threads and also a "ThreadWatcher" thread. This last one calls update() for the corrosponding QWidget while the sort threads are alive and updates QLabels after the sort threads exit. Originally I just had a loop that checked each one and called update() and then slept for 10ms (which worked), but I decided to try to do it all with slots.

    The code below gives the following errors:

    "QObject: Cannot create children for a parent that is in a different thread. (Parent is ThreadWatcher2(0x221b888), parent's thread is QThread(0x3f4ae0), current thread is ThreadWatcher2(0x221b888)" -- on start but this still works fine.

    "QObject::killlTimer: timers cannot be stopped from another thread" -- when I try to pause, this doesn't work.

    Attempting to kill (setting control[1]) doesn't work (here, on the loop-based method it did), but I can probably figure that one out.

    I'll leave out everything before run(), so here's the revelant variables. I also left out the other 5 finished() handlers. If there's some way to create a single handler with an (int) to differentiate which I'd be interested in that as well, as they are all identical except the array index. I'm guessing QOBject::moveToThread() is the solution (if there is one) but I'm unclear on the specifics. updateTimer->moveToThread(this) after allocation in run() doesn't seem to help.

    alive[6] = bool array, marked to false in finished() handler for each thread
    window[6] = references to the QWidgets to call update() on
    thread[6] = array of sort thread references
    control[2] = volatile bool array, [0] for pause and [1] for kill, set in (ommitted) parent thread
    threadsLeft = starts a 6, decremented in each finished() handler
    lock / condition = QMutex / QWaitCondition set by parent to pause

    Qt Code:
    1. void ThreadWatcher2::run() {
    2. updateTimer = new QTimer(this);
    3. connect(updateTimer, SIGNAL(timeout()), this, SLOT(timeoutHandler()));
    4. connect(thread[0], SIGNAL(finished()), this, SLOT(finishedHandler0()));
    5. connect(thread[1], SIGNAL(finished()), this, SLOT(finishedHandler1()));
    6. connect(thread[2], SIGNAL(finished()), this, SLOT(finishedHandler2()));
    7. connect(thread[3], SIGNAL(finished()), this, SLOT(finishedHandler3()));
    8. connect(thread[4], SIGNAL(finished()), this, SLOT(finishedHandler4()));
    9. connect(thread[5], SIGNAL(finished()), this, SLOT(finishedHandler5()));
    10. updateTimer->start(delay);
    11.  
    12. QThread::exec();
    13. status->setText("Status: Done");
    14. }
    15. void ThreadWatcher2::timeoutHandler() {
    16. for (int i=0; i<6; i++) {
    17. if (alive[i])
    18. window[i]->update(); }
    19.  
    20. if (threadsLeft==0) {
    21. QThread::exit(0); }
    22.  
    23. if (control[1])
    24. QThread::exit(1);
    25. else if (control[0]) {
    26. updateTimer->stop();
    27. lock->lock();
    28. condition->wait(lock);
    29. lock->unlock();
    30. updateTimer->start(delay);}
    31. }
    32. void ThreadWatcher2::finishedHandler0() {
    33. disconnect(SIGNAL(finished()), this, SLOT(finishedHandler0()));
    34. alive[0] = false;
    35. threadsLeft--;
    36. seconds[0] = (float)runTime[0] / 1000.0f;
    37. time[0]->setText("Time: "+QString::number(seconds[0],'g',3));
    38. }
    39. void ThreadWatcher2::finishedHandler1() {
    40. .
    41. .
    42. .
    To copy to clipboard, switch view to plain text mode 

    thanks
    Last edited by Galen; 14th April 2010 at 00:59. Reason: more info

Similar Threads

  1. QThread and QTimer
    By sivrisinek in forum Qt Programming
    Replies: 4
    Last Post: 30th April 2009, 16:41
  2. QThread & QTimer
    By hosseinyounesi in forum Qt Programming
    Replies: 5
    Last Post: 13th April 2009, 08:22
  3. QTimer and QThread in Qtopia 4.2.0
    By mellibra in forum Qt for Embedded and Mobile
    Replies: 3
    Last Post: 25th October 2007, 08:26
  4. QTimer and QThread
    By TheKedge in forum Qt Programming
    Replies: 4
    Last Post: 21st September 2006, 14:52
  5. Qthread n QTimer Problem
    By quickNitin in forum Qt Programming
    Replies: 5
    Last Post: 8th June 2006, 14:12

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
  •  
Qt is a trademark of The Qt Company.