在QML应用中,我们经常要用到一个SplashScreen的画面来渲染我们的应用。那么我们怎么在自己的应用中做一个Splash Screen呢?
首先我们来设计一个自己的SplashScreen的QML模块:
import QtQuick 2.0 Item { id: splash anchors.fill: parent property int timeoutInterval: 2000 signal timeout Image { id: splashImage anchors.fill: parent source: "images/splash.jpg" } Timer { interval: timeoutInterval; running: true; repeat: false onTriggered: { visible = false splash.timeout() } } }
import QtQuick 2.0 import Ubuntu.Components 1.1 /*! \brief MainView with a Label and Button elements. */ MainView { // objectName for functional testing purposes (autopilot-qt5) objectName: "mainView" // Note! applicationName needs to match the "name" field of the click manifest applicationName: "splashscreen.liu-xiao-guo" /* This property enables the application to change orientation when the device is rotated. The default is false. */ //automaticOrientation: true // Removes the old toolbar and enables new features of the new header. useDeprecatedToolbar: false width: units.gu(60) height: units.gu(85) Page { title: i18n.tr("Splashscreen") MainWindow { id: mainwindow anchors.fill: parent visible: false } SplashScreen { onTimeout: { console.log("it times out!"); mainwindow.visible = true; } } } }
import QtQuick 2.0 import Ubuntu.Components 1.1 MainView { id: mainView // objectName for functional testing purposes (autopilot-qt5) objectName: "mainView" // Note! applicationName needs to match the "name" field of the click manifest applicationName: "splashscreenqt.liu-xiao-guo" width: units.gu(60) height: units.gu(85) property int timeoutInterval: 2000 signal timeout Page { title: i18n.tr("") Image { id: splashImage anchors.fill: parent source: "images/splash.jpg" } } }
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQuickView> #include <QElapsedTimer> int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQuickView view; view.setResizeMode(QQuickView::SizeRootObjectToView); view.setSource(QUrl(QStringLiteral("qrc:///SplashScreen.qml"))); view.show(); QElapsedTimer t; t.start(); while(t.elapsed()<2000) { QCoreApplication::processEvents(); } view.setSource(QUrl(QStringLiteral("qrc:///Main.qml"))); view.show(); return app.exec(); }