如何将QML映射添加到QGraphicscene?

时间:2018-10-04 04:52:54

标签: qt qml qgraphicsscene qquickview qquickwidget

应用程序将QGraphicscene(mpscene)QGraphicsView(mpGraphicsView)一起使用,我正在尝试向QGraphicscene(mpscene)添加 qml映射对象,但是地图视图像弹出窗口一样单独出现。如何将此地图添加到QGraphicscene(mpscene)

以下是我在其他帖子中建议的几种方法。

方法1

 PlaneTrack *blr2bgm = new PlaneTrack; //Cpp object set to root context
 QQmlEngine *engine = new QQmlEngine(mpGraphicsView);
 QQuickWidget *view = new QQuickWidget(engine, mpGraphicsView);
 view->setResizeMode(QQuickWidget::SizeViewToRootObject);
 view->rootContext()->setContextProperty("blr2bgm",blr2bgm);
 view->setSource(QUrl(QStringLiteral("qrc:/PlaneTrack.qml")));
 mpScene->addWidget(view);    

方法2

 PlaneTrack *blr2bgm = new PlaneTrack; //Cpp object set to root context
 QQuickView *view = new QQuickView();
 view->rootContext()->setContextProperty("blr2bgm",blr2bgm);
 view->setSource(QUrl(QStringLiteral("qrc:/PlaneTrack.qml")));
 view->setResizeMode(QQuickView::SizeRootObjectToView);
 QWidget *container = QWidget::createWindowContainer(view);
 container->setMinimumSize(view->size());
 container->setFocusPolicy(Qt::TabFocus);
 mpScene->addWidget(container);

1 个答案:

答案 0 :(得分:1)

PlaneTrack.qml 文件的 window 容器中,我已更改为 Item 。 以下代码将qml添加到图形场景。

    PlaneTrack *blr2bgm = new PlaneTrack; // cpp class object data interact

    QQuickWidget *view = new QQuickWidget();
    view->rootContext()->setContextProperty("blr2bgm",blr2bgm);
    view->setSource(QUrl(QStringLiteral("qrc:/PlaneTrack.qml")));
    view->setMinimumSize(300, 300);
    view->setResizeMode(QQuickWidget::SizeRootObjectToView);

    mpScene->addWidget(view); // adding to Graphic scene