Results 1 to 6 of 6

Thread: qthread / qtimer

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

    Default qthread / qtimer

    edit: sorry, figured this one out so I'm changing the topic. I'd like to be able to stop and start this qtimer from within the timeoutHandler slot, but doing so gives errors about killing a timer from a different thread. The code below is just an example and in the real deal it would check a shared variable (modified by the parent) for a pause request, then use QMutex and QWaitCondition to control the pause. Does this sound like something that should even be possible in the first place? I've gotten around this by having the parent control the QTimer externally, but I'd rather all the load be in the child thread.

    QObject::killTimer: timers cannot be stopped from another thread"
    QOBject::startTimer: timers cannot be started from another thread"

    Qt Code:
    1. void ThreadWatcher2::run() {
    2. updateTimer = new QTimer();
    3.  
    4. QSignalMapper signalMapper;
    5.  
    6. for (int i=0; i<6; i++) {
    7. connect(thread[i], SIGNAL(finished()), &signalMapper, SLOT(map()));
    8. signalMapper.setMapping(thread[i], i); }
    9.  
    10. connect(&signalMapper, SIGNAL(mapped(int)), this, SLOT(finishedHandler(int)));
    11.  
    12. connect(updateTimer, SIGNAL(timeout()), this, SLOT(timeoutHandler()));
    13.  
    14. updateTimer->start(10);
    15.  
    16. QThread::exec();
    17. }
    18. void ThreadWatcher2::timeoutHandler() {
    19. for (int i=0; i<6; i++)
    20. window[i]->update();
    21.  
    22. updateTimer->stop();
    23. updateTimer->start(10);
    24.  
    25. if (threadsLeft==0)
    26. QThread::exit(0);
    27. }
    28. void ThreadWatcher2::finishedHandler(int t) {
    29. threadsLeft--;
    30. QTextStream cout(stdout, QIODevice::WriteOnly);
    31. cout << "t: " << t << endl;
    32. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Galen; 16th April 2010 at 05:24.

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

    Default Re: qthread / qtimer

    I don't know what your ThreadWatcher2 class does but if I understand it correctly, you don't need it. The main thread can easily do its task. Just connect the finished() signal from each thread to a slot (you don't need a signal mapper) in the main thread, where you will reduce the remaining thread count. When that reaches 0, you will know the task is completed. You shouldn't need to periodically update() your window - the window should be notified it should update itself by something that actually changes what is displayed in the window. The third thing is that you forgot that QThread object doesn't live in a thread it represents so there is a good chance your slots were executed in the context of a wrong thread leading to a race condition you might be experiencing.

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

    Default Re: qthread / qtimer

    Sorry, I realized what was wrong and changed the post (needed to start the threadwatcher before the sort threads or some of the sorters exited before the connections were even made) to something completely different since I can't delete the topic.

    Originally I did have the sorters calling updates, but I realized they would go through the roof when there is no delay, when certainly updating more than 100x a second (which can be accomplished using a 10ms timer) was not necessary. Perhaps I should not be concerned on that count though?

  4. #4
    Join Date
    Apr 2010
    Posts
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qthread / qtimer

    Anyone? I guess my question is do slots not run in the same thread despite being defined in the same class? Or is there some sort of scope issue here?

    thanks

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

    Default Re: qthread / qtimer

    Read my post again, the answer is there.

  6. #6
    Join Date
    Apr 2010
    Posts
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qthread / qtimer

    Oh, right sorry... looks like you discussed this over here too.

    http://www.qtcentre.org/threads/2640...parate-QThread

Similar Threads

  1. get finished signal from a thread
    By ProTonS in forum Qt Programming
    Replies: 4
    Last Post: 21st August 2009, 14:17
  2. QProcess::finished()
    By T1c4L in forum Qt Programming
    Replies: 11
    Last Post: 9th July 2008, 20:06
  3. lost signals ?
    By deepinlife in forum Qt Programming
    Replies: 3
    Last Post: 17th June 2008, 10:11
  4. QThread and signals (linux/UNIX signals not Qt Signals)
    By Micawber in forum Qt Programming
    Replies: 1
    Last Post: 28th November 2007, 22:18
  5. QUrlOperator::copy sends two finished signals
    By hvengel in forum Qt Programming
    Replies: 5
    Last Post: 29th May 2007, 19:42

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.