调用QThread的wait()方法时,报警:QThread::wait: Thread tried to wait on itself
原因:在自己的线程中调用线程的wait()方法。
解决方法:在线程外的其他线程中,调用线程的wait()方法。
示例代码如下:
MyObject.h
#ifndef MYOBJECT_H
#define MYOBJECT_H
#include <QObject>
#include <QThread>
class MyObject : public QObject
{
Q_OBJECT
public:
explicit MyObject(QObject *parent = nullptr);
~MyObject();
void init();
void quitThread();
void test1();
public Q_SLOTS:
void slotTest();
private:
QThread *m_thread = nullptr;
};
#endif // MYOBJECT_H
MyObject.cpp
#include "MyObject.h"
#include <qdebug.h>
MyObject::MyObject(QObject *parent) : QObject(parent)
{
init();
}
MyObject::~MyObject()
{
qDebug() << Q_FUNC_INFO << QThread::currentThreadId();
quitThread();
}
void MyObject::init()
{