QObject::setParent: Cannot set parent, new parent is in a different thread

后端 未结 2 908
滥情空心
滥情空心 2021-01-16 09:49

Greeting

I have a following class.

class MyClass : public QObject
{
    Q_OBJECT

public:
    Q_INVOKABLE QVariant status();

public:
    MyClass(Cla         


        
相关标签:
2条回答
  • 2021-01-16 09:57

    This is wrong in many ways:

    QThread myClassThread;
    MyClass * myClass = new MyClass(classX);
    connect(&myClassThread, SIGNAL(started()), myClass, SLOT(init()));
    myClass->moveToThread(&myClassThread);
    myClassThread.start();
    
    1. You are creating thread object on stack not on heap! This means myClassThread object will be destroyed when this block of code ends.
    2. myClass has a parent. Object which are moved to thread can't have any parent. Only whole three of objects can be moved to threads
    0 讨论(0)
  • 2021-01-16 10:06

    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: Cannot set parent, new parent is in a different thread

    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 revolves around the parent of either myClass or classX objects

    Possibility I : myClass->moveToThread(&myClassThread); is failing

    Cause: myClass has a parent already set. which is forbidden.

    It means 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);
    

    Possibility II : init() is violating thread affinity

    Cause: `classX``or a mysterious related object has a parent already set or is not movable to another thread. Think widget.

    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.

    Which One is Occurring?

    Try putting a break in the debugger, in the controller code and look at the thread id. Then put a break in the debugger in the init method.

    • If it is the same thread, case I
    • Otherwise it is case II
    0 讨论(0)
提交回复
热议问题