如何在 Qt5 中将 QML 文件加载到 QGraphicsScene 中 [英] How to load a QML file into a QGraphicsScene in Qt5

查看:67
本文介绍了如何在 Qt5 中将 QML 文件加载到 QGraphicsScene 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用此代码将 qml 文件加载到 QGraphicsScene 中:

I tried to load a qml file into a QGraphicsScene using this code :

QGraphicsScene* scene = new QGraphicsScene;
QQmlEngine *engine = new QQmlEngine;
QQmlComponent component(engine,"main.qml",QQmlComponent::PreferSynchronous);
qDebug()<<component.errors();
QGraphicsObject *object =
 qobject_cast<QGraphicsObject *>(component.create());
scene->addItem(object);

errors() 不返回任何内容,应用程序输出显示:

errors() returns nothing and the app output says :

QGraphicsScene::addItem: cannot add null item

推荐答案

您不能将 QtQuick2(Qt5 的新版本 QtQuick/QML)与 QGraphicsScene 一起使用.您需要将它与 QQuickView 一起使用.

You can't use QtQuick2 (Qt5's new version of QtQuick/QML) with QGraphicsScene. You need to use it with QQuickView.

如果您需要缩放 QML 文件以使其适合 QQuickView,只需使用 QQuickView::setResizeMode 将根对象调整为您的视图(QQuickView::SizeRootObjectToView>).

If you need to zoom the QML file to make it fit the QQuickView, just use QQuickView::setResizeMode to resize the root object to your view(QQuickView::SizeRootObjectToView).

这篇关于如何在 Qt5 中将 QML 文件加载到 QGraphicsScene 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆