I am using a thread with an external event loop. This thread explains why: Postin custom events to a subclass of QThread
Now I've got a nasty problem with cleaning up timers running in the event loop class.
I've got tons of messages saying:
"QObject::killTimer: timers cannot be stopped from another thread"
when closing the application.

Example code snippets below illustrate the problem:

An example event loop class:
Qt Code:
  1. class MyEventLoop : public QEventLoop
  2. {
  3. public:
  4. MyEventLoop(QObject* parent = 0) : QEventLoop(parent)
  5. {
  6. timers << startTimer(1000);
  7. timers << startTimer(2500);
  8. }
  9.  
  10. ~MyEventLoop()
  11. {
  12. // this basically gets never called since the output is flooded by thousands of lines saying:
  13. // "QObject::killTimer: timers cannot be stopped from another thread"
  14. // and i am forced to kill the application process
  15. foreach (int id, timers)
  16. killTimer(id);
  17. }
  18.  
  19. protected:
  20. void timerEvent(QTimerEvent* event)
  21. {
  22. qDebug() << "ID: " << event->timerId();
  23. }
  24.  
  25. private:
  26. QList<int> timers;
  27. };
To copy to clipboard, switch view to plain text mode 

An example thread class using an "external" event loop:
Qt Code:
  1. class MyThread : public QThread
  2. {
  3. public:
  4. MyThread(QObject* parent = 0) : QThread(parent) {}
  5.  
  6. ~MyThread()
  7. {
  8. myeventloop->quit();
  9. // i have also tried "delete myeventloop;"
  10. myeventloop->deleteLater();
  11. }
  12.  
  13. protected:
  14. void run()
  15. {
  16. myeventloop = new MyEventLoop;
  17. myeventloop->exec();
  18. }
  19.  
  20. private:
  21. MyEventLoop* myeventloop;
  22. };
To copy to clipboard, switch view to plain text mode 

An example widget managing a thread like above:
Qt Code:
  1. class ThreadWidget : public QTextEdit
  2. {
  3. public:
  4. ThreadWidget(QWidget* parent = 0) : QTextEdit(parent)
  5. {
  6. mythread = new MyThread(this);
  7. mythread->start();
  8. }
  9.  
  10. ~ThreadWidget()
  11. {
  12. // as far as i understand, quit() would have no effect
  13. // because the thread is running an external event loop
  14. mythread->terminate();
  15. }
  16.  
  17. private:
  18. MyThread* mythread;
  19. };
To copy to clipboard, switch view to plain text mode