Results 1 to 16 of 16

Thread: QObject with QTimer moved to QThread - QTimer::timeout() not processed until wait()

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2012
    Posts
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default QObject with QTimer moved to QThread - QTimer::timeout() not processed until wait()

    Hi guys!
    I have the following problem:
    1. I want get thread doing some work and update the label in the main thread every second.
    2. I got it working subclassing QThread bu according to some sources it is not a good way.
    3. I decided to subclass a QObject (MyObject) with internal QTimer* _myTimer, and doWork() function with an internal loop, This object is after construction moved to newly created QThread* _thread.

    My problem is that _myTimer's timeout() signal which is connected to tick() slot of the _myObject, is not executed after _thread->start(). tick() is executed only after doing _thread->wait(). Both the tick() slot and myTimer exist in the myThread so i do not know what is the problem. It seems like there is no event loop in myThread.

    Here is my code:
    Qt Code:
    1. class MyObject : QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. MyObject(QObject *parent=0);
    6.  
    7. public slots:
    8. void tick();
    9. void doWork();
    10.  
    11. private:
    12. QTimer* _myTimer;
    13. };
    14.  
    15. void MyObject::doWork()
    16. {
    17. _myTimer = new QTimer(this);
    18. _myTimer->setInterval(1000);
    19. connect(_myTimer, SIGNAL(timeout()), this, SLOT(tick())/*,Qt::DirectConnection*/);
    20. _myTimer->start()
    21. qDebug() << QThread::currentThreadId() << "doWork()";
    22. while(..)
    23. {
    24. ...
    25. }
    26. }
    27.  
    28. void MyObject::tick()
    29. {
    30. emit tickSignalToGUI(..);
    31. qDebug() << QThread::currentThreadId() << "tick()";
    32. }
    33.  
    34. // and somewhere in GUI thread
    35. (...)
    36. _thread = new QThread(this);
    37. _myObject = new MyObject(); /
    38. _myObject->moveToThread(_thread);
    39. _thread->start();
    40. // _myObject->doWork(); //this blocks execution
    41. QMetaObject::invokeMethod(_myObject,"doWork");
    42. qDebug() << QThread::currentThreadId() << "GUI thread";
    43. (...)
    To copy to clipboard, switch view to plain text mode 

    Any ideas what is wrong with this approach?

    best regards,
    Last edited by airproject; 4th August 2012 at 14:09.

Similar Threads

  1. QTimer in QThread doesn't start or timeout
    By Boron in forum Qt Programming
    Replies: 9
    Last Post: 21st October 2011, 13:51
  2. Get QTimer's seconds till next timeout?
    By hakermania in forum Newbie
    Replies: 12
    Last Post: 12th February 2011, 23:49
  3. QTimer delayed timeout
    By DavidY in forum Qt Programming
    Replies: 4
    Last Post: 10th December 2009, 21:34
  4. Replies: 2
    Last Post: 9th September 2009, 00:26
  5. QThread/QObject and QTimer
    By swiety in forum Qt Programming
    Replies: 2
    Last Post: 25th January 2008, 08:37

Tags for this Thread

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.