[英]QQuickView as item in a QGraphicsScene
I would like to integrate a QQuickView in a QGraphicsView, but I only get a gray rectangle.我想在 QGraphicsView 中集成 QQuickView,但我只得到一个灰色矩形。
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.我知道将 qml 集成到 qgraphicsview 中是一个可怕的选择,但我的团队已经在这个应用程序上工作了 3 年,并且不可能更改主要的 QGraphicsView。 I just want to know if there is a way to include some QML now.
我只想知道现在是否有办法包含一些 QML。
Maybe there is a solution for this?也许有解决方案? Maybe I just have to forget about it.
也许我只需要忘记它。 Maybe you know.
也许你知道。 Thanks !
谢谢 !
Code sample代码示例
main.cpp主.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测试.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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.