ending thread at end of app
-
Hi all -
I've seen some other postings on this, but none are quite focused on my issue.
Recently, when I close an app by pressing the "X" in the upper right corner of my main widget (this is Window 10), I started getting an error message:
QThread: Destroyed while thread is still runningIf I'm in the debugger, it really throws a shoe, asking me (twice) if I want to debug the process, etc.
I'm not entirely sure what's going on here. Here's some of my main code:
QApplication a(argc, argv); ModelDevice modelDevices(nullptr); QThread thread(&a); worker = new Worker(&modelDevices); worker->moveToThread(&thread); QObject::connect(&widget, &Widget::quitButtonPushed, &thread, &QThread::exit); QObject::connect(&thread, &QThread::finished, &a, &QApplication::quit); QObject::connect(&thread, &QThread::finished, worker, &Worker::deleteLater);I had the idea of having the widget destructor signal the thread to quit:
// this doesn't compile QObject::connect(&widget, &Widget::sendTerminate, &thread, QThread::quit);But I get a warning about a call to non-static member function without an object argument.
So:
- would this be the right way to do this if I can get that line fixed?
- what's wrong with that attempted connection? It seems to me that thread is the object, so I don't understand the error message.
Thanks...
-
If your QThread is only in main.cpp, the normal way is
int main(int argc, char *argv[]) { QApplication a(argc, argv); ... int code = a.exec(); thread.quit();//do something to quit the thread, this depends on how your Worker is working. If it is blocking, you need to find a way to tell it to stop thread.wait();//wait the thread to finish return code; }