How can I implement a custom QEventLoop in my multimedia timer driven thread? I have tried creating my own QEventLoop and calling processEvents() on it with every iteration of the timer. But it doesn't do anything. Well, if I don't use the moveToThread() call in the code below, then the dataIn() connection is a direct one and the signal and the slot are executed on the same thread. If I do the moveToThread(), then the connection should be a queued one, but I guess it goes to the wrong thread. How can I tell Qt to handle the slot with the provessEvents() call of my own QEventLoop?


Qt Code:
  1. class WorkerThread : QThread
  2. {
  3. QEventLoop* eventLoop; // my custom event loop
  4. Data data;
  5. void tick();
  6.  
  7. public slots:
  8. dataIn(Data);
  9. }
  10.  
  11. WorkerThread::WorkerThread()
  12. {
  13. moveToThread(this); // optional
  14. }
  15.  
  16. // This is the callback function of the windows media timer.
  17. // It just calls tick() of my worker thread.
  18. void CALLBACK PeriodicCallback(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
  19. {
  20. WorkerThread* wt = (WorkerThread*)dwUser;
  21. wt->tick();
  22. }
  23.  
  24. // When the thread is started, I create the MM timer and have it call PeriodicCallback().
  25. void RobotControl::start()
  26. {
  27. // Set resolution to the minimum supported by the system.
  28. TIMECAPS tc;
  29. timeGetDevCaps(&tc, sizeof(TIMECAPS));
  30. timerRes = qMin(qMax(tc.wPeriodMin, (UINT) 0), tc.wPeriodMax);
  31. timeBeginPeriod(timerRes);
  32.  
  33. // Create the callback timer.
  34. timerId = timeSetEvent(12, 0, PeriodicCallback, (DWORD_PTR) this, 1);
  35. }
  36.  
  37. WorkerThread::tick()
  38. {
  39. // Initialize and call the event loop.
  40. static bool eventLoopInitialized = false;
  41. if (!eventLoopInitialized)
  42. {
  43. eventLoopInitialized = true;
  44. eventLoop = new QEventLoop();
  45. }
  46. else
  47. {
  48. //QApplication::processEvents();
  49. eventLoop->processEvents(); // not sure which one to call
  50. }
  51.  
  52. use(data);
  53. qDebug() << QThread::currentThreadId() << "worker thread was ticked";
  54. }
  55.  
  56. // I would like this slot to be handled on the same thread as tick().
  57. WorkerThread::dataIn(Data d)
  58. {
  59. data = d;
  60. qDebug() << QThread::currentThreadId() << "data received";
  61. }
To copy to clipboard, switch view to plain text mode