简体   繁体   中英

QQuickView as item in a QGraphicsScene

I would like to integrate a QQuickView in a QGraphicsView, but I only get a gray rectangle.

I know this is a horrible choice to integrate qml into qgraphicsview, but my team is working on this application for 3 years now and it is not possible to change the main QGraphicsView. I just want to know if there is a way to include some QML now.

Maybe there is a solution for this? Maybe I just have to forget about it. Maybe you know. Thanks !

Code sample

main.cpp

#include <QApplication>
#include <QMainWindow>
#include <QtQuick/QQuickView>
#include <QGraphicsView>
#include <QGraphicsScene>

// Main
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QGraphicsScene * scene = new QGraphicsScene;
    QGraphicsView * view = new QGraphicsView(scene);

    QQuickView * quickView = new QQuickView;
    quickView->setSource(QUrl::fromLocalFile("test.qml")); // Displays a text
    QWidget * container = QWidget::createWindowContainer(quickView);
    container->setFixedSize(500, 100);

    scene->addWidget(container);
    scene->addEllipse(50, 50, 100, 100); // Just to know my scene is correctly drawn

    view->show();

    return a.exec();
}

test.qml

import QtQuick 2.15

Rectangle {

    width: 500
    height: 100
    
    Text {
        text: "This is a text in a QML view"
        anchors.left: parent.left
        anchors.top: parent.top
    }
    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM