QThread: Destroyed while thread is still running
时间: 2024-01-28 18:40:17 浏览: 142
pyqt5 的多线程(QThread)遇到的坑(二)
This error message occurs when a QThread object is destroyed while the thread it represents is still running. This can cause issues such as memory leaks and unpredictable behavior.
To avoid this error, you should ensure that the QThread object is deleted only after the thread has finished running. You can do this by connecting the QThread::finished() signal to a slot that deletes the QThread object.
For example:
```cpp
QThread* thread = new QThread();
MyObject* object = new MyObject();
// Move the object to the thread
object->moveToThread(thread);
// Start the thread
thread->start();
// Connect the finished() signal to a slot that deletes the thread
QObject::connect(thread, &QThread::finished, thread, &QThread::deleteLater);
// Do some work with the object
object->doWork();
// Wait for the thread to finish
thread->wait();
// The thread has finished, so delete the object
delete object;
```
In this example, the QThread object is deleted only after the thread has finished running, ensuring that there are no issues with the thread object being destroyed while the thread is still running.
阅读全文