QQuickWindow上下文属性?

Raf*_*SCS 5 c++ qt window qml

我通过以下代码创建一个新窗口:

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();
Run Code Online (Sandbox Code Playgroud)

此代码将MyController属性设置为rootContext,这意味着根上下文中的所有页面都可以访问此属性.所以,这不允许我从同一个QML文件中打开2个不同的窗口,每个窗口都有自己的MainController实例.

问题:如何将此MyController属性绑定到QQuickWindow上下文而不是引擎的rootContext?

我试过用QQuickView做这样的事情:

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();
Run Code Online (Sandbox Code Playgroud)

但我抱怨以下消息:

"QQuickView仅支持加载从QQuickItem派生的根对象.

如果您的示例使用QML 2(例如qmlscene)和您加载的.qml文件具有'import QtQuick 1.0'或'import Qt 4.7',则会发生此错误.

要使用'import QtQuick 1.0'或'import Qt 4.7'加载文件,请使用Qt Quick 1模块中的QDeclarativeView类."

fxa*_*xam 12

QQuickWindow 不拥有自己的任何上下文属性,因此无法使用它设置上下文属性.

就像消息解释,QQuickView期望从继承了QML根对象QQuickItem,这意味着根可以是Rectangle,Item等,但不ApplicationWindowWindow,因为它们不从继承QQuickItem.如果您可以将根对象更改为类似的对象Rectangle,则可以创建不同的实例QQuickView来实现目标,因为如果您不为其提供现有对象,则每个实例都会QQuickView创建自己的实例.QQmlEngineQQmlEngine

QQmlApplicationEngine实际上继承自QQmlEngine,因此每个实例都有自己的根上下文.例如,调用以下createMain()两次将创建两个具有单独引擎的窗口,单独的控制器和单独的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();
}
Run Code Online (Sandbox Code Playgroud)

如果您不想创建多个副本QQmlAppicationEngine,则可以共享一个实例QQmlApplicationEngine,并为每个窗口实例创建子上下文:

// 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();
}
Run Code Online (Sandbox Code Playgroud)

当然QQmlApplication,QQmlEngine如果没有使用QQmlApplicationEngine的功能,你可以改为.