Yes. mAppEngine is instance of QQmlApplicationEngine. I would like to assign one model to one ListView(in case of many tabs created). I tried this:
SmsModel *model = new SmsModel(); model->createDummyData(); QObject* pRoot = mAppEngine->rootObjects()[0]; QObject* m_pTabView= pRoot->findChildren<QObject*>("conversationTabView").first(); if (m_pTabView) { QVariant returnedValue; QVariant title = phoneNumber; QQmlContext *context = new QQmlContext(mAppEngine, mAppEngine); context->setContextProperty(QString("myModel"), model); mAppEngine->rootContext()->setContextProperty("mmm", model); QQmlComponent *component = new QQmlComponent(mAppEngine, QUrl("qrc:/ChatView.qml"), this); QObject *object = component->create(context); QQmlEngine::setObjectOwnership(object, QQmlEngine::CppOwnership); object->setProperty("active", QVariant(true)); component->setParent(m_pTabView); QMetaObject::invokeMethod(m_pTabView, "addTab", Q_RETURN_ARG(QVariant, returnedValue), Q_ARG(QVariant, title), Q_ARG(QVariant, QVariant::fromValue(component))); object->setProperty("anchors.fill", "parent"); }and in QML:
Rectangle { ... ListView { model: myModel } }I get this error "ReferenceError: myModel is not defined". When I do
mAppEngine->rootContext()->setContextProperty("myModel", model);I have data in GUI, but when I create two tabs, then both ListViews share the same model(I want one model <-> one ListView).