0
votes

I tried to use QQmlComponent component(view.engine(), QUrl::fromLocalFile("MyItem.qml")); and then QOObject *object = component.create(); but it gives me Qml Component not ready. Further tried to connect the statusChanged signal to a slot function, but it doesnt seem to load the new qml components.

QQuickView view;
view.setSource(QUrl(QStringLiteral("qrc:/main.qml")));
QQmlComponent component(view.engine(), QUrl::fromLocalFile("MyItem.qml"));
QObject *object = component.create();
object->setParent(view.rootObject());
view.show()
2
Post the code you have tried. - cmannett85
QQuickView view; view.setSource(QUrl(QStringLiteral("qrc:/main.qml"))); QObject *rootObj = view.rootObject(); QQmlComponent component(view.engine(), QUrl::fromLocalFile("MyItem.qml")); QObject *object = component.create(); object->setParent(view.rootObject()); view.show() - Cartik Sharma
I've also tried it by creating a class VViewerQml, in the constructor of the class, _view.setSource(QUrl(QStringLiteral("qrc:/main.qml"))); connect(&_view, SIGNAL(statusChanged(QQuickView::Status)), this, SLOT(onViewStatusChanged(QQuickView::Status))); _view.show(); and in the slot onViewStatusChanged(QQuickView::Status status) {if (status) { QQmlComponent *c = new QQmlComponent(_view.engine(), QUrl::fromLocalFile("MyItem.qml")); QQuickItem *i = qobject_cast<QQuickItem*>(c->create()); QQuickItem *i = qobject_cast<QQuickItem*>(c->create()); // add i to view.rootObject - Cartik Sharma

2 Answers

0
votes

If you're loading from a local file, then set the QQmlComponent to load synchronously by specifying it in the constructor:

QQmlComponent component(view.engine(),
                        QUrl::fromLocalFile("MyItem.qml"),
                        QQmlComponent::PreferSynchronous );
0
votes

See Interacting with QML Objects from C++.

Loading QML Objects from C++

A QML document can be loaded with QQmlComponent or QQuickView. QQmlComponent loads a QML document as a C++ object that can then be modified from C++ code. QQuickView also does this, but as QQuickView is a QWindow-derived class, the loaded object will also be rendered into a visual display; QQuickView is generally used to integrate a displayable QML object into an application's user interface.

...

Also, more wide topic Integrating QML and C++.