Results 1 to 3 of 3

Thread: Using QTimer in QThreads

  1. #1
    Join Date
    Mar 2009
    Posts
    2
    Thanks
    1

    Default Using QTimer in QThreads

    Hi, I just started learning Qt several days ago, and I got stuck on this for a while now. I'm trying to create a new thread in which a QTimer would run, and connect the timer's timeout() signal to a function running in that thread. Here is one of the things that I tried:

    I try to run a timer here connected to a slot in MyObject (onTimeOut)
    Qt Code:
    1. class MyThread : public QThread
    2. {
    3. public:
    4. MyThread();
    5. ~MyThread();
    6.  
    7. QTimer *timer;
    8. void run();
    9. };
    10.  
    11. void MyThread::run()
    12. {
    13. MyObject object;
    14. timer = new QTimer;
    15. connect(timer, SIGNAL(timeout()), &object, SLOT(onTimeOut()));
    16. timer->start(500);
    17. exec();
    18. }
    To copy to clipboard, switch view to plain text mode 
    To my understanding this should call onTimeOut every half second, but that's not happening. Is there something that I'm missing completely?

  2. #2
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Using QTimer in QThreads

    Hi,

    Does "MyObject" class inherit from QObject and has declared the macro "Q_OBJECT" ?
    What is returning the "connect" statment? just add a boolean value as return value and also check the console output.
    Òscar Llarch i Galán

  3. The following user says thank you to ^NyAw^ for this useful post:

    tzhu (18th March 2009)

  4. #3
    Join Date
    Mar 2009
    Posts
    2
    Thanks
    1

    Default Re: Using QTimer in QThreads

    Doh I did forget Q_OBJECT in MyObject! Now I'm embarassed to have wasted 2 hours on that...

Similar Threads

  1. Replies: 8
    Last Post: 27th March 2013, 11:51
  2. Extending QTimer()
    By rishid in forum Qt Programming
    Replies: 3
    Last Post: 7th August 2009, 01:59
  3. QThread/QObject and QTimer
    By swiety in forum Qt Programming
    Replies: 2
    Last Post: 25th January 2008, 08:37
  4. Replies: 5
    Last Post: 6th March 2007, 05:34
  5. QTimer problem ... it runs but never triggs
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 4th July 2006, 12:54

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.