I have a problem with the event loop in a QThread. Say I have a class "Player" that inherits QThread. In the run() function, I have an infinite loop where the thread is put to sleep. Once it's woken up, it can do different things depending on the state of the class. One possibility is to call member functions of another class "Playback", which makes use of a QTimer, so I need to call the exec() function of the thread. Once the Playback's timer is finished with its stuff, it emits a signal that is caught by the thread class, which then calls quit() to get out of the event loop, and puts the thread to sleep again. This works very well when it is called the first time. On the second time, exec() returns immediately and no event loop is entered.

Is it possible to call exec() of a QThread repeatedly (but not concurrently)? In other words, can the following calling sequence be modified to work in a QThread class?:
exec() , quit(), exec()

Maybe by somehow "resetting" the event loop after the quit()?

Excerpt from the code:

Qt Code:
  1. // Player inherits QThread
  2. void Player::run()
  3. {
  4. mutex.lock();
  5. playback = new Playback();
  6. QObject::connect(playback, SIGNAL(finishedPlayback()), this, SLOT(done()));
  7. mutex.unlock();
  8.  
  9. while(1) {
  10. mutex.lock();
  11. cond.wait(&mutex);
  12. mutex.unlock();
  13.  
  14. if(abort) {
  15. mutex.lock();
  16. delete playback;
  17. playback = NULL;
  18. mutex.unlock();
  19. return;
  20. }
  21.  
  22. if(status == Forward) {
  23. // This call will start a QTimer, and emit finishedPlayback() when done
  24. playback->forward(startTimestamp, endTimestamp, timestep);
  25. exec();
  26. }
  27. }
  28. }
  29.  
  30. void Player::done(void)
  31. {
  32. mutex.lock();
  33. status = Finished;
  34. mutex.unlock();
  35. quit();
  36. }
To copy to clipboard, switch view to plain text mode