QQuickWindow Context Property?

I create a new window through the following code:

QMainController* myController = new QMainController(0,m_autenticado); QQmlApplicationEngine* engine = new QQmlApplicationEngine(this); engine->rootContext()->setContextProperty("MyController", myController); engine->load(QUrl(QStringLiteral("qrc:///newPage.qml"))); QQuickWindow* window = qobject_cast<QQuickWindow*>(engine->rootObjects().at(0)); window->showFullScreen(); 

This code sets the MyController property for rootContext, which means that all pages in the root context will have access to this property. Thus, this will not allow me to open 2 different windows from the same QML file, each with its own instance of MainController.

QUESTION How can I bind this MyController property to a QQuickWindow context instead of the engine root server?

I tried using QQuickView and did like this:

 QMainController* myController = new QMainController(0,m_autenticado); QQuickView* view = new QQuickView(); view->rootContext()->setContextProperty("MyController", myController ); view->setSource(QUrl(QStringLiteral("qrc:///main.qml"))); view->showFullScreen(); 

But I complained about the following message:

"QQuickView only supports loading root objects derived from QQuickItem.

If your example uses QML 2 (for example, qmlscene) and the .qml file you downloaded has โ€œimport QtQuick 1.0โ€ or โ€œimport Qt 4.7โ€, this error will occur.

To upload files using "import QtQuick 1.0" or "import Qt 4.7", use the QDeclarativeView in the Qt Quick 1 module.

+5
source share
1 answer

QQuickWindow does not have its own context property, so there is no way to set a context property with it.

As the message explains, QQuickView expects a QML root object that inherits from QQuickItem , which means that root can be a Rectangle , Item , etc., but not ApplicationWindow or Window , since they are not inherited from QQuickItem . If you can change the root object to something like a Rectangle , you can create different instances of QQuickView to achieve your goal, since each QQuickView creates its own QQmlEngine , unless you provide an existing QQmlEngine for it.

QQmlApplicationEngine actually inherits from QQmlEngine , so each instance has its own root context. For example, calling the following createMain() twice will create two windows with separate engines, a separate controller and a separate rootContext:

 void foo::createMain() { //TODO: Set a proper parent to myController QMainController* myController = new QMainController(0,m_autenticado); QQmlApplicationEngine* engine = new QQmlApplicationEngine(this); engine->rootContext()->setContextProperty("MyController", myController); engine->load(QUrl(QStringLiteral("qrc:///newPage.qml"))); QQuickWindow* window = qobject_cast<QQuickWindow*>(engine->rootObjects().at(0)); window->showFullScreen(); } 

If you are not sure about creating multiple copies of QQmlAppicationEngine , you can share one instance of QQmlApplicationEngine and create a child context for each instance of the window:

 // foo.h class foo { private: QQmlApplicationEngine m_engine; // can change to QQmlEngine if you prefer } // foo.cpp void foo::createMain() { //TODO: Set a proper parent to myController QMainController* myController = new QMainController(0,m_autenticado); // Create a new context as a child of m_engine.rootContext() QQmlContext *childContext = new QQmlContext(&m_engine, &m_engine); childContext->setContextProperty("MyController", myController); QQmlComponent *component = new QQmlComponent(&m_engine, &m_engine); component->loadUrl(QUrl("qrc:///qml/newPage.qml")); // Create component in child context QObject *o = component->create(childContext); QQuickWindow* window = qobject_cast<QQuickWindow*>(o); window->show(); } 

And of course, you can change QQmlApplication to QQmlEngine if there are no QQmlApplicationEngine features.

+10
source

Source: https://habr.com/ru/post/1202172/


All Articles