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:
{
public:
{
timers << startTimer(1000);
timers << startTimer(2500);
}
~MyEventLoop()
{
// this basically gets never called since the output is flooded by thousands of lines saying:
// "QObject::killTimer: timers cannot be stopped from another thread"
// and i am forced to kill the application process
foreach (int id, timers)
killTimer(id);
}
protected:
{
qDebug() << "ID: " << event->timerId();
}
private:
QList<int> timers;
};
class MyEventLoop : public QEventLoop
{
public:
MyEventLoop(QObject* parent = 0) : QEventLoop(parent)
{
timers << startTimer(1000);
timers << startTimer(2500);
}
~MyEventLoop()
{
// this basically gets never called since the output is flooded by thousands of lines saying:
// "QObject::killTimer: timers cannot be stopped from another thread"
// and i am forced to kill the application process
foreach (int id, timers)
killTimer(id);
}
protected:
void timerEvent(QTimerEvent* event)
{
qDebug() << "ID: " << event->timerId();
}
private:
QList<int> timers;
};
To copy to clipboard, switch view to plain text mode
An example thread class using an "external" event loop:
{
public:
~MyThread()
{
myeventloop->quit();
// i have also tried "delete myeventloop;"
myeventloop->deleteLater();
}
protected:
void run()
{
myeventloop = new MyEventLoop;
myeventloop->exec();
}
private:
MyEventLoop* myeventloop;
};
class MyThread : public QThread
{
public:
MyThread(QObject* parent = 0) : QThread(parent) {}
~MyThread()
{
myeventloop->quit();
// i have also tried "delete myeventloop;"
myeventloop->deleteLater();
}
protected:
void run()
{
myeventloop = new MyEventLoop;
myeventloop->exec();
}
private:
MyEventLoop* myeventloop;
};
To copy to clipboard, switch view to plain text mode
An example widget managing a thread like above:
{
public:
{
mythread = new MyThread(this);
mythread->start();
}
~ThreadWidget()
{
// as far as i understand, quit() would have no effect
// because the thread is running an external event loop
mythread->terminate();
}
private:
MyThread* mythread;
};
class ThreadWidget : public QTextEdit
{
public:
ThreadWidget(QWidget* parent = 0) : QTextEdit(parent)
{
mythread = new MyThread(this);
mythread->start();
}
~ThreadWidget()
{
// as far as i understand, quit() would have no effect
// because the thread is running an external event loop
mythread->terminate();
}
private:
MyThread* mythread;
};
To copy to clipboard, switch view to plain text mode
Bookmarks