Introduce
There are two ways to implement thread according to official documentation and the first one is recommended
How to quit a thread correctly
Inherit QObject
workerThread.quit();
workerThread.wait();
Inherit QThread
Thread::Thread()
: m_finishThread(false)
{
}
Thread::~Thread()
{
m_finishThread = true;
this->quit();
this->wait();
}
Thread::run()
{
while (!m_finishThread)
{
...
}
}
Be careful
If you use the first solution, make sure worker class parent is nullptr otherwise it's still the main thread. You can use QThread::currentThreadId() to verify it. By the way, remember to connect QThread::finished() to delete the worker class since it doesn't have a parent
In terms of the QThread class, please make sure the thread is not running after closing the application