【QML】qml splash 启动界面 实现代码

1. 工程结构

在这里插入图片描述

2. 现象

在这里插入图片描述
在这里插入图片描述

3. 代码

3.1 main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>


int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
        &app, [url](QObject *obj, const QUrl &objUrl) {
            if (!obj && url == objUrl)
                QCoreApplication::exit(-1);
        }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}

3.2 main.qml

import QtQuick 2.15
import QtQuick.Window 2.15

QtObject {
    id: root
    property QtObject $splashScreen: Splash{}

    property var loader: Loader{
        asynchronous: true
        source: "qrc:/MainView.qml"
        active: false
        onLoaded: {
            $splashScreen.delay();
        }
    }

    Component.onCompleted:{
        loader.active = true;
    }
}

3.3 MainView.qml

import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15

ApplicationWindow {
    id: window
    visible: true
    width: 800
    height: 600
    title: qsTr("Splash Demo")
    flags: Qt.Window | Qt.FramelessWindowHint

    Button{
        anchors{
            top: parent.top;
            right: parent.right;
            margins: 5
        }
        text: "X"
        width: 50
        height: 50
        onClicked: Qt.quit();
    }

    Text{
        text: qsTr("Test window");
        anchors.centerIn: parent
        font.pointSize: 30
    }

    Component.onCompleted: window.show()
}

3.4 Splash.qml

import QtQuick 2.15
import QtQuick.Window 2.15

Window {
    id: splash
    color: "transparent"
    title: "Splash Window"
    modality: Qt.ApplicationModal
    flags: Qt.SplashScreen | Qt.WindowStaysOnTopHint
    x: (Screen.width - splashImage.width) / 2
    y: (Screen.height - splashImage.height) / 2
    width: splashImage.width
    height: splashImage.height

    Image {
        id: splashImage
        source: "qrc:/background.png"
    }

    Text{
        id: textCtrl
        width: contentWidth
        height: contentHeight
        anchors{
            left: splashImage.left;
            bottom: splashImage.bottom
        }
        font.pointSize: 30
    }

    Timer {
        id: timer
        interval: 1000;
        running: false;
        repeat: false
        onTriggered: {
            splash.visible = false;
        }
    }

    Component.onCompleted: {
        splash.show()
    }

    function delay(){
        timer.start();
    }
}

4. 参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值