qml在开发板上启动时,出现白屏(qml的两种加载方式)

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include<QTextCodec>
#include <QQuickView>
#include <QtQml>

QQuickView *view;
int main(int argc, char *argv[])
{
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));

    myapplication app(argc, argv);


    view = new QQuickView;
    view->setSource(QUrl(QStringLiteral("qrc:/main.qml")));
    view->show();

//    QQmlApplicationEngine engine;
//    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    QObject::connect(view->engine(), SIGNAL(quit()), view, SLOT(close()));

    return app.exec();
}

上面是采用QQuickView的方式(Rectangle加载方式)。这种加载一切正常,启动过程中没有出现白屏现象(此时退出Qt程序需要将QQuickView close掉才行,执行Qt.quit() 会触发QQmlEngine的quit()信号)

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include<QTextCodec>
#include <QQuickView>
#include <QtQml>

QQuickView *view;
int main(int argc, char *argv[])
{
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));

    myapplication app(argc, argv);
    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    return app.exec();
}

上面的是采用的是QQmlApplicationEngine方式(window加载方式),启动时会先出现白屏,然后才会出现想要的界面。

两种不同的方式实现的效果的细微差异–查看帮助文档个人感觉是由于,QQuickView只是提供一个界面,然后可以在界面添加各种组件,所以显示的时候就是qml加载成功的时候,没成功之前是就是黑的(屏幕原先是什么样还是什么样)。
而QQuickWindow(window)却是为我们提供了一个基于graphical QML scene的窗口,主要是方便scene中的组件(QQuickItems)之间互相interact(交互)。所以在使用的时候,qml还没有加载完毕,scene已经存在,就是我们看到的一片白色。。。

QML 3D场景可以通过两种方式进行加载: 1. 直接在QML文件中定义场景。这种方式适用于简单的场景,可以通过使用QML的3D元素(如 Box、Sphere、Cylinder 等)来创建3D模型和场景。 例如,以下代码创建了一个简单的场景,其中包含一个盒子和一个球体: ``` import Qt3D.Core 2.12 import Qt3D.Render 2.12 import Qt3D.Extras 2.12 Entity { id: rootEntity components: [ RenderSettings { activeFrameGraph: ForwardRenderer { clearColor: "transparent" } }, Camera { id: camera projectionType: CameraLens.PerspectiveProjection fieldOfView: 45 aspectRatio: 16/9 nearPlane : 0.1 farPlane : 1000.0 position: Qt.vector3d(0.0, 0.0, 40.0) upVector: Qt.vector3d(0.0, 1.0, 0.0) viewCenter: Qt.vector3d(0.0, 0.0, 0.0) } ] BoxMesh { id: boxMesh width: 5.0 height: 5.0 depth: 5.0 } SphereMesh { id: sphereMesh radius: 2.0 } Transform { id: boxTransform translation: Qt.vector3d(-10.0, 0.0, 0.0) } Transform { id: sphereTransform translation: Qt.vector3d(10.0, 0.0, 0.0) } Material { id: material effect: PhongMaterial { ambient: Qt.rgba(0.1, 0.1, 0.1, 1.0) diffuse: Qt.rgba(1.0, 1.0, 1.0, 1.0) specular: Qt.rgba(0.3, 0.3, 0.3, 1.0) shininess: 100.0 } } PhongMaterial { id: material2 ambient: Qt.rgba(0.1, 0.1, 0.1, 1.0) diffuse: Qt.rgba(1.0, 1.0, 1.0, 1.0) specular: Qt.rgba(0.3, 0.3, 0.3, 1.0) shininess: 100.0 } Entity { id: boxEntity components: [ boxMesh, material, boxTransform ] } Entity { id: sphereEntity components: [ sphereMesh, material2, sphereTransform ] } } ``` 2. 通过使用Qt3DSceneLoader加载外部3D模型文件。这种方式适用于复杂的场景,可以使用专业的3D建模软件(如Blender、Maya等)创建模型和场景,并将其导出为支持Qt3D的格式(如.gltf、.obj等)。 例如,以下代码使用Qt3DSceneLoader加载一个外部的.gltf文件: ``` import Qt3D.Core 2.12 import Qt3D.Render 2.12 import Qt3D.Extras 2.12 Entity { id: rootEntity components: [ RenderSettings { activeFrameGraph: ForwardRenderer { clearColor: "transparent" } }, Camera { id: camera projectionType: CameraLens.PerspectiveProjection fieldOfView: 45 aspectRatio: 16/9 nearPlane : 0.1 farPlane : 1000.0 position: Qt.vector3d(0.0, 0.0, 40.0) upVector: Qt.vector3d(0.0, 1.0, 0.0) viewCenter: Qt.vector3d(0.0, 0.0, 0.0) } ] Entity { id: externalEntity Qt3DSceneLoader { id: loader source: "path/to/external/file.gltf" } onStatusChanged: { if (loader.status === Qt3DRender.Qt3DRender.Loaded) { rootEntity.addComponent(externalEntity) } } } } ``` 以上是两种加载QML 3D场景的方式,您可以根据自己的需求进行选择。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

庐州李大爷

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值