Hello!

I have a GUI and a worker thread. The worker thread is ticked periodically by a windows multimedia timer. I'm not using a QTimer because I need a higher precision. Now the problem is that I want to transfer data from the GUI to the worker thread in a thread safe manner using signals and slots. But the multimedia timer creates its own thread, so the worker loop is in a different thread than the event loop, where the slots are handled. I don't know how to solve this, please help. Here is the code:

Qt Code:
  1. /////////////// The GUI class///////////////////
  2. class GUI
  3. {
  4. WorkerThread wt;
  5. Data data;
  6.  
  7. signals:
  8. dataOut(Data);
  9. }
  10.  
  11. GUI::GUI()
  12. {
  13. connect(this, SIGNAL(dataOut(Data)), &wt, SLOT(dataIn(Data)));
  14. wt.start();
  15. }
  16.  
  17. GUI::somewhere()
  18. {
  19. emit dataOut(data);
  20. qDebug() << QThread::currentThreadId() << "data emitted";
  21. }
  22.  
  23.  
  24. /////////////// The Worker Thread ///////////////////
  25. class WorkerThread : QThread
  26. {
  27. Data data;
  28. void tick();
  29.  
  30. public slots:
  31. dataIn(Data);
  32. }
  33.  
  34. WorkerThread::WorkerThread()
  35. {
  36. moveToThread(this);
  37. }
  38.  
  39. // This is the callback function of the windows media timer.
  40. void CALLBACK PeriodicCallback(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
  41. {
  42. WorkerThread* wt = (WorkerThread*)dwUser;
  43. wt->tick();
  44. }
  45.  
  46. // When the thread is started, I create the MM timer and start the event loop.
  47. void RobotControl::run()
  48. {
  49. // Set resolution to the minimum supported by the system.
  50. TIMECAPS tc;
  51. timeGetDevCaps(&tc, sizeof(TIMECAPS));
  52. timerRes = qMin(qMax(tc.wPeriodMin, (UINT) 0), tc.wPeriodMax);
  53. timeBeginPeriod(timerRes);
  54.  
  55. // Create the callback timer.
  56. timerId = timeSetEvent(12, 0, PeriodicCallback, (DWORD_PTR) this, 1);
  57.  
  58. // Start the event loop.
  59. exec();
  60. }
  61.  
  62. WorkerThread::tick()
  63. {
  64. use(data);
  65. qDebug() << QThread::currentThreadId() << "worker thread was ticked";
  66. }
  67.  
  68. WorkerThread::dataIn(Data d)
  69. {
  70. data = d;
  71. qDebug() << QThread::currentThreadId() << "data received";
  72. }
To copy to clipboard, switch view to plain text mode 

Now the output of this code is:
0xaa8 worker thread was ticked
0xaa8 worker thread was ticked
0xaa8 worker thread was ticked
0xb98 data emitted
0x16e4 data received
0xaa8 worker thread was ticked
0xaa8 worker thread was ticked
0xaa8 worker thread was ticked
As you can see, there are three different threads. How can I achieve that the tick()s are executed on the same thread as the data slots?