如何使用 QML 创建启动屏幕

2024-01-09

我正在尝试使用 QT 开发 Android 应用程序。 我想在应用程序启动时显示启动屏幕。启动画面将停留 2 秒钟,然后将显示应用程序的主页。 为此,我创建了 2 个 .qml 文件。

启动画面.qml

import QtQuick 2.9
import QtQuick.Controls 2.2
import QtQuick.Window 2.3

Window {
    id: window
    visible: true
    width: Screen.width
    height: Screen.height
    signal timeout

    Image {
        id: image
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.verticalCenter: parent.verticalCenter
        width: 300
        height: 300
        source: "qrc:/../Desktop/photo_2018-03-21_19-53-06.jpg"
    }

    Text {
        id: text1
        y: image.height + image.y + 20
        text: qsTr("@startimeahmet Presents")
        anchors.horizontalCenter: parent.horizontalCenter
        font.pixelSize: 25
    }

    Timer {
        interval: 2000; running: true; repeat: false
        onTriggered: {
            visible = false
            window.timeout()
        }
    }
}

main.qml

import QtQuick 2.9
import QtQuick.Controls 2.2
import QtQuick.Window 2.3

ApplicationWindow {
    id: root
    visible: false
    width: Screen.width
    height: Screen.height

    Splash {
        onTimeout: root.visible = true
    }
}

但这行不通。对此的任何帮助表示赞赏。

附注我正在使用 QT 5.11.1 和 QT Creator 4.6.2


使用原生 Android 启动屏幕。

  1. 在中创建启动资源android/res/drawable/splash.xml。就像是
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque">
    <item>
        <shape android:shape="rectangle" >
            <solid android:color="#ffffff"/>
        </shape>
    </item>    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/app"/>
    </item>
</layer-list>
  1. 创建主题于android/res/values/apptheme.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="@android:style/Theme.DeviceDefault.NoActionBar">
        <item name="android:background">@drawable/splash</item>
        <item name="android:statusBarColor">#ffffff</item>
    </style>
</resources>
  1. 在 android/AndroidManifest.xml 中找到activity元素并添加此属性:android:theme="@style/AppTheme"添加这些:

    <meta-data android:name="android.app.splash_screen_drawable" android:resource="@drawable/splash"/>
    <meta-data android:name="android.app.splash_screen_sticky" android:value="true"/>
    
  2. 在你的 .pro 文件中添加

    QT += androidextras

  3. 当您的应用程序准备就绪时,在您的 C++ 代码中添加以下行:

    QtAndroid::隐藏SplashScreen(250);

  4. Enjoy!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 QML 创建启动屏幕 的相关文章

随机推荐