QObject :: setParent:无法设置父级,新的父级在另一个线程中 [英] QObject::setParent: Cannot set parent, new parent is in a different thread
问题描述
问候
我正在上一个课.
class MyClass : public QObject
{
Q_OBJECT
public:
Q_INVOKABLE QVariant status();
public:
MyClass(ClassX * classX);
public slots:
void slotA();
void slotB();
signals:
void signalA();
void signalB();
private:
void init();
void doSomething();
private:
ClassX * classX;
ClassA classA;
ClassB classB;
};
在MyClass
的构造函数中,我将classX
设置为this.classX
,在init()
中,我将connect
的某些classX
信号设置为MyClass
插槽,反之亦然,在someFunction()
中我使用classA
和classB
.
In MyClass
's constructor, I set classX
to this.classX
and in init()
, I connect
some of classX
signals to MyClass
slots and wise-versa and in someFunction()
i use classA
and classB
.
在主线程的controller
类中,创建MyClass
对象并在其他线程中运行它.
In my controller
class in main thread, I create MyClass
object and run it inside different thread.
MyClass * myClass = new MyClass(classX);
connect(&myClassThread, SIGNAL(started()), myClass, SLOT(init()));
myClass->moveToThread(&myClassThread);
myClassThread.start();
我在qDebugger
中看到以下警告.
QObject::setParent: Cannot set parent, new parent is in a different thread
谁能告诉我为什么我得到那个警告?
Can anyone tell me why i get that warning ?
感谢进阶
PS 1:在主线程中创建的classX
.
PS 1: The classX
created in main thread.
PS 2:请记住,一切正常,我没有任何问题,我只想知道此警告的原因以及解决方法.
PS 2 : Remember, Everything work fine and i don't have any problem, I just want to know the reason of this warning and how to fix it.
PS 3:我还在主线程中使用以下命令来显示javascript
中的对象.
PS 3 : I also use the following command in main thread to expose the object in javascript
.
webFrame->addToJavaScriptWindowObject("myClassObject", myClass);
QThread myClassThread是类成员.
Edit 1 : QThread myClassThread is class member.
我相信信息匮乏,你们感到困惑,对此我感到抱歉.
Edit 2 : I believe the lack of information, confused you guys and i'm sorry about that.
MyClass
的构造函数是这样的:
The constructor of MyClass
is like this :
MyClass::MyClass(ClassX * classX)
{
this.classX = classX;
}
推荐答案
让我们说清楚. 您的代码无法正常运行.这就是框架告诉您的内容.
Let's be clear. Your code is not working as you intended it to work. That is what the framework is telling you.
QObject :: setParent:无法设置父级,新的父级位于另一个 线程
QObject::setParent: Cannot set parent, new parent is in a different thread
这意味着某个对象(怀疑为myClass
)的所有插槽和信号都不会在与预期线程相同的线程中执行.这里的问题与myClass
和classX
对象的父对象有关
This means all slots and signals of a certain object (suspected myClass
) will not be executed in the same thread as the one expected. The issue here revolve about the parent of either myClass
andclassX
objects
可能性I:myClass-> moveToThread(& myClassThread);失败
原因: myClass已设置了一个父项.这是禁止的.
Cause: myClass has a parent already set. which is forbidden.
这意味着init()
将由线程对象myClassThread
的线程触发.就线程和事件而言,这几乎与您做的
It mean that init()
will be triggered by the thread of the thread object myClassThread
. Thread-wise and event-wise, this is almost the same as if you did
MyClass * myClass = new MyClass(classX);
QMetaObject::invokeMethod(myClass, "init", Qt::QueuedConnection);
可能性II:init()违反了线程亲和力
原因: "classX"或一个神秘的相关对象已设置了父对象或无法移动到另一个线程.思考小部件.
Cause: `classX``or a mysterious related object has a parent already set or is not movable to another thread. Think widget.
moveToThread
成功,您在一个线程中具有MyClass
,在另一个线程中具有classX
.构造myClass
时已提供classX
. myClass
现在正在操纵另一个线程中的对象,并且如果没有进一步的代码,我们将无法承担线程安全性或正确的子父亲缘关系.仔细检查MyClass
:: MyClass`和MyClass :: init.
moveToThread
succeed, you have MyClass
in one thread, and classX
in another thread. You have provided classX
when constructing myClass
. myClass
is now manipulating an object in another thread, and without further code we cannot assume thread safety or correct child parent affinity. Review MyClass
::MyClass` and MyClass::init carefully.
发生了什么?
在调试器中的控制器代码中中断,然后查看线程ID.然后使用init
方法插入调试器.
Break in the debugger in controller code and look at the thread id. Then break in the debugger in the init
method.
- 如果是同一线程,情况一
- 否则就是案例
这篇关于QObject :: setParent:无法设置父级,新的父级在另一个线程中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!