Mostly for future visitors, I did manage to get this working. Following is a tiny code fragment that highlights the solution:
QQmlContext* pluginContext = new QQmlContext(mView->engine());
QmlComponent* component(new QQmlComponent(mView->engine(), "/PATH/TO/QML.qml"));
pluginContext->setContextProperty("model", myModelObject);
compRoot = component->create(pluginContext);
if (component->isReady()) {
QQuickItem* item = qobject_cast<QQuickItem*>(compRoot);
QPointF point
(posHelper.
getLeft(), posHelper.
getTop());
QSizeF size
(posHelper.
getWidth(), posHelper.
getHeight());
item->setParentItem(mRoot); // The item pointer obtained through QQuickView.rootObject
item->setPosition(point);
item->setSize(size);
qDebug() << "Card (" << posHelper.getCurRow() << ", " << posHelper.getCurCol() << "): " <<
"(" << point.x() << ", " << point.y() << ") - " <<
"(" << size.width() << ", " << size.height() << ")";
return component;
} else {
qWarning() << "Failed to create component from C++:" << component->errorString();
}
}
QQmlContext* pluginContext = new QQmlContext(mView->engine());
QmlComponent* component(new QQmlComponent(mView->engine(), "/PATH/TO/QML.qml"));
QObject* compRoot = NULL;
pluginContext->setContextProperty("model", myModelObject);
compRoot = component->create(pluginContext);
if (component->isReady()) {
QQuickItem* item = qobject_cast<QQuickItem*>(compRoot);
QPointF point(posHelper.getLeft(), posHelper.getTop());
QSizeF size(posHelper.getWidth(), posHelper.getHeight());
item->setParentItem(mRoot); // The item pointer obtained through QQuickView.rootObject
item->setPosition(point);
item->setSize(size);
qDebug() << "Card (" << posHelper.getCurRow() << ", " << posHelper.getCurCol() << "): " <<
"(" << point.x() << ", " << point.y() << ") - " <<
"(" << size.width() << ", " << size.height() << ")";
return component;
} else {
qWarning() << "Failed to create component from C++:" << component->errorString();
}
}
To copy to clipboard, switch view to plain text mode
The thing that did the trick was:
item->setParentItem(mRoot)
item->setParentItem(mRoot)
To copy to clipboard, switch view to plain text mode
Bookmarks