如何使用 QML 创建启动画面

sta*_*met 1 qt splash-screen qml

我正在尝试使用 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()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

主文件

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
    }
}
Run Code Online (Sandbox Code Playgroud)

但这不起作用。对此的任何帮助表示赞赏。

ps 我使用 QT 5.11.1 和 QT Creator 4.6.2

小智 5

使用原生 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>
Run Code Online (Sandbox Code Playgroud)
  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>
Run Code Online (Sandbox Code Playgroud)
  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"/>
    
    Run Code Online (Sandbox Code Playgroud)
  2. 在您的 .pro 文件中添加

    QT += androidextras

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

    QtAndroid::hideSplashScreen(250);

  4. 享受!