This is how I did it, please go through it and suggest any changes or different approaches for doing the same.
QtQuick2ApplicationViewer *viewer = QtQuick2ApplicationViewer::instance();
QQmlContext *context = viewer->rootContext();
context->setContextProperty("_client", &client);
/*Main QML File*/
viewer->setMainQmlFile(QStringLiteral("qml/qml-dynamic/main.qml"));
viewer->showExpanded();
QQmlEngine *engine = viewer->engine();
/*dynamically loaded component*/
QQmlComponent component(engine, QUrl::fromLocalFile("qml/qml-dynamic/dyn-comp.qml"));
QObject *object = component.create();
QQuickItem *item = qobject_cast<QQuickItem*>(object);
/*In qml, items won't get drawn unless they are parented to the view.*/
/* You should always use QObject::setProperty(), QDeclarativeProperty
* or QMetaProperty::write() to change a QML property value, to ensure
* the QML engine is made aware of the property change.
*/
object->setProperty("parent", QVariant::fromValue<QObject*>(viewer->rootObject()));
/*This works same as setProperty*/
// QQmlProperty::write(object, "parent"
// , QVariant::fromValue<QObject*>(viewer->rootObject()));
QQmlEngine::setObjectOwnership(object, QQmlEngine::CppOwnership);
/*Set X and Y properties of the item created.*/
item->setx(150);
item->setY(150);
↧










