在做QT开发时,运行程序出现 如下警告:
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QThread(0xb95feffd70), parent's thread is QThread(0x1d3729aef20), current thread is QThread(0xb95feffd70)
按字面意思可以理解为: 不能为属于不同线程的父对象创建子对象。由第二行提示可以看出来,当前线程与父对象属于的线程不是同一个线程,所以出现警告了。我们可以先看一个非常简单的例子,就很好理解了:
#ifndef THREADTEST2_H
#define THREADTEST2_H
#include
#include
class Class1 : public QObject
{
public:
Class1(QObject* parent) : QObject(parent){}
};
class ThreadTest2 : public QThread
{
public:
ThreadTest2()
{
qDebug() << "ThreadTest2::ThreadTest2, thread = " << QThread::currentThread();
}
void run() override
{
qDebug() << "ThreadTest2::run, thread = " << QThread::currentThread();
qDebug() << "ThreadTest2::run, ThreadTest2 thread = " << this->thread();
m_class = new Class1(this);
}
private:
Class1* m_class = nullptr;
};
#endif // THREADTEST2_H
#include
#include "qthreadtest.h"
#include "threadtest2.h"
#include
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
ThreadTest2 thread2;
thread2.start();
return app.exec();
}
运行结果:
ThreadTest2::ThreadTest2, thread = QThread(0x2864244cbd0)
ThreadTest2::run, thread = QThread(0x8d9d4ff7a8)
ThreadTest2::run, ThreadTest2 thread = QThread(0x2864244cbd0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QThread(0x8d9d4ff7a8), parent's thread is QThread(0x2864244cbd0), current thread is QThread(0x8d9d4ff7a8)
由运行结果看出 thread2(ThreadTest2)属于主线程(QThread(0x2864244cbd0)),我们尝试在 QThread(0x2864244cbd0)线程中为属于(QThread(0x2864244cbd0))的 thread2 对象创建子对象,所以出问题了。
那么我们可以如何避免出现这种问题呢,方法有很多种,下面介绍几种:
既然问题出在为父对象创建子对象过程中,那么我们创建对象时不指定父对象不就好了,修改如下:
#ifndef THREADTEST2_H
#define THREADTEST2_H
#include
#include
class Class1 : public QObject
{
public:
Class1(QObject* parent = nullptr) : QObject(parent){}
};
class ThreadTest2 : public QThread
{
public:
ThreadTest2()
{
qDebug() << "ThreadTest2::ThreadTest2, thread = " << QThread::currentThread();
}
void run() override
{
qDebug() << "ThreadTest2::run, thread = " << QThread::currentThread();
qDebug() << "ThreadTest2::run, ThreadTest2 thread = " << this->thread();
m_class = new Class1();
}
private:
Class1* m_class = nullptr;
};
#endif // THREADTEST2_H
显然这种方式不是很好,不指定父对象,创建的m_class对象的释放就要由自己管理了。我们可以看看其他方法。
既然是因为在不同的线程创建子对象出问题了,那么我们将父对象属于的线程与执行线程设置为同一个线程不就好了吗。修改如下:
#include
#include "qthreadtest.h"
#include "threadtest2.h"
#include
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
ThreadTest2 thread2;
thread2.moveToThread(&thread2);
thread2.start();
return app.exec();
}
运行结果:
ThreadTest2::ThreadTest2, thread = QThread(0x20f230c0d80)
ThreadTest2::run, thread = QThread(0xb5db97fb58)
ThreadTest2::run, ThreadTest2 thread = QThread(0xb5db97fb58)
可以看到thread2绑定的线程变为QThread(0xb5db97fb58),与run执行线程属于同一个线程了。
既然是因为在不同的线程创建子对象出问题了,我们可以将子对象的创建放到父对象绑定的线程中创建就好了,就上面例子而言简单的方法就是将子对象的创建放到父对象的构造函数中:
#ifndef THREADTEST2_H
#define THREADTEST2_H
#include
#include
class Class1 : public QObject
{
public:
Class1(QObject* parent = nullptr) : QObject(parent){}
};
class ThreadTest2 : public QThread
{
public:
ThreadTest2()
{
m_class = new Class1(this);
qDebug() << "ThreadTest2::ThreadTest2, thread = " << QThread::currentThread();
}
void run() override
{
qDebug() << "ThreadTest2::run, thread = " << QThread::currentThread();
qDebug() << "ThreadTest2::run, ThreadTest2 thread = " << this->thread();
}
private:
Class1* m_class = nullptr;
};
#endif // THREADTEST2_H
只要理解问题是怎么产生的,如何避免,之后再复杂的项目,也知道该如何解决啦。