Results 1 to 10 of 10

Thread: QTimer in QThread doesn't start or timeout

  1. #1
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    229
    Thanks
    2
    Thanked 29 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QTimer in QThread doesn't start or timeout

    Hi there,

    I am playing around with QThreads for the first time. And now I have the weird problem that the QTimer in my thread class doesn't start or emit the timeout() signal. Some code:
    Qt Code:
    1. CanThread::QCanThread( QObject* parent )
    2. : QThread( parent )
    3. {
    4. m_countTimer = new QTimer( this );
    5. m_countTimer->setInterval( 1000 );
    6. connect( m_countTimer, SIGNAL(timeout()), this, SLOT(updateCounter()) );
    7. }
    8.  
    9. void CanThread::run() {
    10. m_countTimer->start();
    11. exec();
    12. }
    13.  
    14. void CanThread::stop() {
    15. m_countTimer->stop();
    16. this->quit();
    17. }
    18.  
    19. void QCanThread::updateCounter() {
    20. m_currentCounter++;
    21. emit showCurrentCounter( m_currentCounter );
    22. }
    To copy to clipboard, switch view to plain text mode 
    The timer m_countTimer shall timeout all 1000ms. As reaction a counter variable is incremented and the GUI is signalled to display that variable.
    (I know it is bullshit to do this that way. A simple timer and the GUI without my CanThread would be enough, but I want get a feeling how deal with threads)

    But the slot updateCounter() is never called. Either the timer doesn't start when I call threadObject->start() or the timer doesn't emit its signal timeout().
    What is wrong with my code?

  2. #2
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTimer in QThread doesn't start or timeout

    couple of things:

    1. QCanThread? just a typo?
    2. catch the value returned by connect and see if the signal is connected to slot

    rest looks fine

  3. #3
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    229
    Thanks
    2
    Thanked 29 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTimer in QThread doesn't start or timeout

    1. I thought I had removed all 'Q's not to confuse anyone .
    2. connect() returns "true".

    If I add a
    QTimer::singleShot(1000, this, SLOT(updateCounter()));
    after
    m_countTimer->start();
    the GUI displays the counter value after a second. But the interval timer still doesn't work. Now I am confused .

  4. #4
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTimer in QThread doesn't start or timeout

    strange..ok, try checkin the timer ids in constructor and run method..another thing u can try is move all the code from constructor to run method..just for playin around

  5. #5
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    229
    Thanks
    2
    Thanked 29 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTimer in QThread doesn't start or timeout

    The Qt doc tells the timer ID is -1 while timer is not running. So the ID is -1 in my constructor.
    After calling start() the ID is a positive number.

    When I create and set the interval of the timer in the run() method it works as expected. My GUI is constantly increasing the value.

    So there must be a difference in timer objects created in thread constructors and timers created later (e.g. after starting the thread).

  6. #6
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTimer in QThread doesn't start or timeout

    actually thats how threads are supposed to work..whatever code u wanna execute should be in run method..thats when a thread actually kicks off..in every documentation they say, derive from base class and implement run..i believe if u connect in run() and do other initialization in constructor, it should work too..

  7. #7
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    229
    Thanks
    2
    Thanked 29 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTimer in QThread doesn't start or timeout

    No. Timer creation has to be done in run(). If only connect() is done in run() it doesn't work.

  8. #8
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTimer in QThread doesn't start or timeout

    hmm..this whole situation is definitely a bit weird..cuz the documentation should mention that any kind of initialization done in the constructor would not take any effect when the thread is actually run..and although it makes sense that run method should have all the initialization, one can always make a mistake of initializing in constructor being a C++ developer....any guru thoughts on this?

  9. #9
    Join Date
    Jul 2010
    Location
    Fortaleza, Brazil
    Posts
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTimer in QThread doesn't start or timeout

    I don't know if you sove that, but that maybe help others.

    Call in class constructor:
    this->moveToThread(this);
    countTimer->moveToThread(this);

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

    Default Re: QTimer in QThread doesn't start or timeout


    Added after 4 minutes:

    hmm..this whole situation is definitely a bit weird..cuz the documentation should mention that any kind of initialization done in the constructor would not take any effect when the thread is actually run..and although it makes sense that run method should have all the initialization, one can always make a mistake of initializing in constructor being a C++ developer....any guru thoughts on this?
    this is because what you code you do in constructor is done in the calling thread. The code in run will be executed in the new thread after start() is called. timers dont work well across thread boundaries so you either need to instantiate the timer in run() or use moveTothread() in run to move the timer.

    Quote Originally Posted by israelins View Post
    I don't know if you sove that, but that maybe help others.

    Call in class constructor:
    this->moveToThread(this);
    countTimer->moveToThread(this);
    that is ridiculous :-/
    Last edited by amleto; 21st October 2011 at 13:49.

Similar Threads

  1. Replies: 8
    Last Post: 27th March 2013, 11:51
  2. 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
  3. QThread : run() Vs start()
    By cs_raja in forum Qt Programming
    Replies: 4
    Last Post: 21st November 2006, 10:36
  4. QTimer and QThread
    By TheKedge in forum Qt Programming
    Replies: 4
    Last Post: 21st September 2006, 14:52
  5. 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.