Results 1 to 3 of 3

Thread: QThread/QObject and QTimer

  1. #1
    Join Date
    Oct 2007
    Location
    Poland
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QThread/QObject and QTimer

    I'm using QTimer in QThread( or QObject ) class :
    Qt Code:
    1. class InfoThread: public QThreed
    2. {
    3. Q_OBJECT
    4. ...
    5. void run();
    6. signals:
    7. void newInfoAvalible( /* some data */ );
    8. private slots:
    9. void update();
    10. ..
    11. private:
    12. QTimer *p_timer;
    13. };
    14.  
    15. InfoThread::Infothread( QObject *parent ) : QThread( parent )
    16. {
    17. p_timer = new QTimer( this );
    18. connect( p_timer, SIGNAL( timeout() ), this, SLOT( update() ) );
    19. }
    20.  
    21. void InfoThread::run()
    22. {
    23. p_timer->start( 1000 );
    24. exec(); // event loop for QTimer
    25. }
    26.  
    27. void InfoThread::update()
    28. {
    29. ...
    30. emit newInfoAvalible( /* some data */ );
    31. }
    To copy to clipboard, switch view to plain text mode 

    For me code is good, but signal newInfoAvalible( /* some data */ ) causes apps to crash. Wher's the problem?

  2. #2
    Join Date
    Jan 2008
    Location
    Silicon valley
    Posts
    15
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QThread/QObject and QTimer

    Is newInfoAvalible() connected to a slot in another QThread?
    If so, does the /*some data*/ contain pointers to something which are invalid after you leave this function? If so, this is expected, as signals between threads use "queued connections".

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QThread/QObject and QTimer

    The QTimer object must be created in InfoThread::run() without a parent (preferably on the stack since QThread::exec() blocks) and the connection must be forced as direct
    Qt Code:
    1. connect( &timer, SIGNAL( timeout() ), this, SLOT( update() ), Qt::DirectConnection );
    To copy to clipboard, switch view to plain text mode 
    because QThread itself lives in different thread than what's being executed in QThread::run().
    J-P Nurmi

Similar Threads

  1. QTimer single shot and too busy app.
    By Pier in forum Qt Programming
    Replies: 3
    Last Post: 20th March 2007, 14:02
  2. Replies: 5
    Last Post: 6th March 2007, 05:34
  3. using QTimer in staticlib
    By jeetu_happy in forum Qt Programming
    Replies: 4
    Last Post: 22nd January 2007, 09:34
  4. QTimer problem ... it runs but never triggs
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 4th July 2006, 12:54
  5. QTimer issue
    By qball2k5 in forum Qt Programming
    Replies: 1
    Last Post: 31st March 2006, 18:14

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.