Results 1 to 20 of 39

Thread: Using QTimer in a QThread

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Using QTimer in a QThread

    I don't get it. I want to start a thread that periodically executes a function using a timer.

    Qt Code:
    1. Thread::Thread(QObject *parent) : QThread(parent)
    2. {
    3. start();
    4. }
    5.  
    6. void Thread::run()
    7. {
    8. QTimer t;
    9. connect(&t, SIGNAL(timeout()), this, SLOT(tick()));
    10. t.start(100);
    11. //t.moveToThread(this);
    12. exec();
    13. }
    14.  
    15. void Thread::tick()
    16. {
    17. qDebug() << currentThreadId() << "tick()";
    18. }
    19.  
    20.  
    21.  
    22. class ThreadTest : public QWidget
    23. {
    24.  
    25. Thread thread;
    26.  
    27. ...
    28. };
    29.  
    30. ThreadTest::ThreadTest(QWidget *parent)
    31. : QWidget(parent)
    32. {
    33. qDebug() << QThread::currentThreadId() << "is the main thread.";
    34. }
    To copy to clipboard, switch view to plain text mode 

    And the output I get is:
    Qt Code:
    1. 0x12b8 is the main thread.
    2. 0x12b8 tick()
    3. 0x12b8 tick()
    4. 0x12b8 tick()
    5. 0x12b8 tick()
    6. ...
    To copy to clipboard, switch view to plain text mode 

    Obviously, the timer is still running on the main thread. Using the moveToThread() doesn't make a difference. What's not right about this?

    Changing the timer to an infinite loop with sleep() works though.

    Qt Code:
    1. void Thread::run()
    2. {
    3. forever
    4. {
    5. tick();
    6. sleep(1);
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. 0xa04 is the main thread.
    2. 0xfa0 tick()
    3. 0xfa0 tick()
    4. 0xfa0 tick()
    5. ...
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Using QTimer in a QThread

    Problem is that you are doing that wrong!
    Qt Code:
    1. class TestClass : QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. TestClass(QObject *parent=0);
    6.  
    7. public slots:
    8. void tick();
    9. };
    10.  
    11. TestClass::TestClass(QObject *parent) : QObject(parent)
    12. {
    13. QTimer *t = new QTimer(this);
    14. connect(t, SIGNAL(timeout()), this, SLOT(tick()));
    15. }
    16.  
    17. void TestClass::tick()
    18. {
    19. qDebug() << currentThreadId() << "tick()";
    20. }
    21.  
    22. // and somewhere
    23. QThread *thread = new QThread(something);
    24. TestClass *object = new TestClass(); // can't pass parent!!!!!!
    25. object->moveToThread(thread);
    26. thread->start();
    27.  
    28. ....
    29. delete object;
    30. thread->WaitFor();
    31. // delete thread;
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Using QTimer in a QThread

    Problem is that you are doing that wrong!
    I don't see what he is doing wrong, in respect to the link you posted, except for what he tried in the second bit, which is wrong because you need to be in the "other thread" to call moveToThread().
    The link you provided is talking about using moveToThread() wrong usage, and that you should not always use a subclass of QThread for your threads.
    But there are many cases where this is the RIGHT way to go.
    Even if from an OOP design point of view this is wrong, from implementation point of view this still should work (the original code with out moveToThread()).
    And if it doesn't we need to understand why.

    His code is almost the same as the example code QThread docs, just that he uses a QTimer and not a QTcpSocket.
    Since the QTimer is allocated in run() its should have the thread affinity of the new thread, and from what the OP shows, it looks like its affinity is to the main GUI thread, which is surprising.

    My guess is that it has to do with calling start() in the thread constructor.
    Try removing the start() form the constructor, and call it normally from outside, see if this changes things.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. QTimer within QThread
    By Eos Pengwern in forum Qt Programming
    Replies: 6
    Last Post: 23rd February 2011, 20:00
  2. qthread / qtimer
    By Galen in forum Qt Programming
    Replies: 5
    Last Post: 17th April 2010, 22:57
  3. QThread and QTimer
    By sivrisinek in forum Qt Programming
    Replies: 4
    Last Post: 30th April 2009, 16:41
  4. QThread & QTimer
    By hosseinyounesi in forum Qt Programming
    Replies: 5
    Last Post: 13th April 2009, 08:22
  5. QTimer and QThread
    By TheKedge in forum Qt Programming
    Replies: 4
    Last Post: 21st September 2006, 14:52

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.