使用 qml+QQuickView 作为初始屏幕不起作用

Use qml+QQuickView as a splash screen NOT working

本文关键字:屏幕 不起作用 qml+QQuickView 使用      更新时间:2023-10-16

我想为我的 c++/qt/qml 应用程序添加一个启动画面。我添加了以下代码:

int main(int argc, char *argv[])
{

    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;
    QQuickView *view = new QQuickView;
    view->setSource(QUrl(QLatin1String("qrc:/Splash.qml")));
    view->show();

   engine.load(QUrl(QLatin1String("qrc:/main.qml")));
   view->close();
   app.exec();
   return 0;

}

和 Splash.qml 文件:

import QtQuick 2.0
import QtQuick.Controls 2.1
Item {
    visible: true
    width: 640
    height: 480
    BusyIndicator {
        anchors.centerIn: parent
        width: 100
        height: 100
        running: true
        Component.onCompleted: {
            console.log("splash screen... " + x + " " + y + " " + width + "x" + height)
            visible=true
        }
    }
}

当应用程序启动时,消息:"qml:初始屏幕...打印了270 190 100x100",但QQuickWindow只是白色的。main.qml 的主窗口可以正常工作。我试图在 Splash.qml 中加载图像,但同样的问题。而且,我不能使用QQuickView::setWindowFlags将其设置为无框窗口。我正在研究win7 64位操作系统。

您可以在 QML 中轻松做到这一点:

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Window 2.0
Window {
    id: mainWindow
    width: 600
    height: 400
    visible: false
    x: Screen.width / 2 - width / 2;
    y: Screen.height / 2 - height / 2;
    Window {
        id: splashWindow
        width: 300
        height: 200
        visible: true
        x: Screen.width / 2 - width / 2;
        y: Screen.height / 2 - height / 2;
        flags: Qt.SplashScreen;
        Rectangle {
            anchors.fill: parent
            color: "green";
        }
        ProgressBar {
            id: progressBar
            anchors.centerIn: parent
            anchors.margins: 20
            value: 0.01
        }
        Timer {
            id: timer
            interval: 50
            repeat: true
            running: true
            onTriggered: {
                progressBar.value += 0.01
                if(progressBar.value >= 1.0) {
                    timer.stop();
                    mainWindow.visible = true;
                    splashWindow.destroy();
                }
            }
        }
    }
}