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