Results 1 to 5 of 5

Thread: QTimer and QThread

  1. #1
    Join Date
    Jan 2006
    Location
    Munich, Germany.
    Posts
    111
    Thanks
    29
    Thanked 3 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default QTimer and QThread

    Hello,
    I've got QThread sub-classed with run() re-implemented thus:
    Qt Code:
    1. void Logger::run()
    2. {
    3. QTimer* timer = new QTimer();
    4. timer->setInterval(updateIntervalSeconds*1000);
    5. connect(timer, SIGNAL(timeout()), this, SLOT(doJob()));
    6. timer->start();
    7.  
    8. exec();
    9. timer->stop();
    10.  
    11. delete timer;
    12. return;
    13. }
    To copy to clipboard, switch view to plain text mode 
    where doJob() is a func that contains stuff to be done. The doJob() func gets called as excpected but when ending the thread I get a crash that points to the timer->stop(). Commenting that out the crash points at the delete timer; commenting that out, the crash points to run()s end brace.

    I get a Qt message QThread object destroyed while thread is still running, but I call the quit() func before deleting the object. what's going on?

    thanks
    K

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany.
    Posts
    111
    Thanks
    29
    Thanked 3 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QTimer and QThread

    Sorry, I think I have it.
    in my destructor I call quit(), apparently, I need also to call wait() before letting the destructor finish.

    thanks
    K

  3. #3
    Join Date
    Sep 2006
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTimer and QThread

    Hello,
    I think You can improve your run() function in the following way:

    Qt Code:
    1. void Logger::run()
    2. {
    3. QTimer timer;
    4. timer.setInterval(updateIntervalSeconds*1000);
    5. connect(&timer, SIGNAL(timeout()), this, SLOT(doJob()));
    6.  
    7. timer.start();
    8.  
    9. exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    I think it is better to create QTimer object on the stack. This object will be destroyed automatically when the run() function finishes. When You call exec() - evet loop is started at this point.

    Br,
    Poter
    Last edited by jacek; 20th September 2006 at 15:49. Reason: missing [code] tags

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany.
    Posts
    111
    Thanks
    29
    Thanked 3 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QTimer and QThread

    ok,
    I've see that the doJob() function is executed in main (gui) thread. Have I set up my timer incorrectly?
    By the way, I need the job executed every second or so, and I just wanted to use a timer instead of having a while loop with a sleep().

    how do I get the timer firing so that doJob() runs in the new thread?

    thanks
    K

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTimer and QThread

    Quote Originally Posted by TheKedge View Post
    how do I get the timer firing so that doJob() runs in the new thread?
    Try:
    Qt Code:
    1. connect( &timer, SIGNAL( timeout() ), this, SLOT( doJob() ), Qt::DirectConnection );
    To copy to clipboard, switch view to plain text mode 
    or move doJob() to another class that you will instantiate in run(), since the Logger instance itself lives in the GUI thread.

  6. The following user says thank you to jacek for this useful post:

    TheKedge (21st September 2006)

Similar Threads

  1. QThread
    By TheKedge in forum Qt Programming
    Replies: 8
    Last Post: 25th August 2006, 10:29
  2. Qthread n QTimer Problem
    By quickNitin in forum Qt Programming
    Replies: 5
    Last Post: 8th June 2006, 14:12
  3. Replies: 6
    Last Post: 17th March 2006, 17:48

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.