Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Qml QObject::moveToThread() perform metaCall
I have a problem with the following situation:
class MyObject : public QObject
Q_PROPERTY(QString componentUrl ...);
class MyJobObj : public QObject
: thread_(new QThread(this))
, job_(new MyJobObj())
connect(this, SIGNAL(callJobStart(QObject*)), job_, SLOT(performOperation(QObject*)));
connect(job_, SIGNAL(operationDone(QObject*)), this, SLOT(threadCompleted(QObject*)));
QQmlEngine* engine = QQmlEngine::contextForObject(this)->engine();
QQmlComponent component(engine, QUrl::fromLocalFile(componentUrl));
auto obj_ = component_->create(QQmlEngine::contextForObject(worker_));
// perform some modification on obj_ via function
// perform some meta method call on obj_
void MyObject::threadCompleted(QObject* obj_)
// Append the obj_ to child and start using it intyo Qml
void MyJobObj::performOperation(QObject* obj_)
// perform computation asynchronoous on obj_
// perform metaCall on obj_
I got a problem with the metaCall, crash, both main thread and created thread are inside (see the image attached for an idea of the call stack at crash).
I'm wondering if the QQmlContext and QQmlEngine for the object must be changed or something?
Is that a possible way to pass QObject to another thread, or should I use something different here?
Is a QObject from Qml can be move to other thread or is that forbidden?
If anybody can shed some light on this, I would be really be grateful, Thank
Found the problem, I was using the engine and context from the main thread for the component instance. Creating a new QQmlEngine and using it's rootContext for the Component instance solve the problem. Probably, this object context will remain valid as long as the Engine owner exist. In my case the MyObject is the new QQmlEngine owner and use the returned object to set some outputs.