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

后端 未结 2 940
滥情空心
滥情空心 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

提交回复
热议问题