使用qml + QQuickView作为启动画面不工作

时间:2017-05-27 08:46:44

标签: c++ qt qml

我想在我的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:splash screen ... 270 190 100x100”,但QQuickWindow只是白色。 main.qml的主窗口可以正常工作。我试图在Splash.qml中加载图像,但同样的问题。并且,我无法使用QQuickView :: setWindowFlags使其成为无框架窗口。我正在研究win7 64位操作系统。

1 个答案:

答案 0 :(得分:0)

你可以在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();
                }
            }
        }
    }
}