QML闪屏无法正常工作 [英] QML Splash Screen Not Working

查看:27
本文介绍了QML闪屏无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在最新的Belle更新之前,我的应用程序可以运行,但现在不能了。只有当我将init.qml文件和plashcreen.qml文件上的com.nokia.symbian 1.1降级到1.0,但不显示main.qml文件时,启动画面才会起作用。当我指示main.cpp直接加载main.qml时,该应用程序可以运行…。我迷路了!以下是我为main.cpp:

提供的代码
   #include <QtGui/QApplication>
   #include "qmlapplicationviewer.h"

   Q_DECL_EXPORT int main(int argc, char *argv[])
   {
QScopedPointer<QApplication> app(createApplication(argc, argv));

QmlApplicationViewer viewer;
viewer.setOrientation(QmlApplicationViewer::ScreenOrientationLockPortrait);
viewer.setSource(QUrl("qrc:/qml/SmartFlyer/init.qml"));
//viewer.setMainQmlFile(QLatin1String("qrc:qml/SmartFlyer/main.qml"));
viewer.showExpanded();

return app->exec();
   }

对于init.qml:

   import QtQuick 1.1
   import com.nokia.symbian 1.1

   Item {
       id: init

       SplashScreen {
           id: splash
           show: true              // show splash
           minTimeout: 3000         // show splash at least for 3 sec
    image: "data/splash_screen.png"  // path to splash image
    canFinish: false         // change to true when main QML will be loaded
    z: 100                   // highest page.
}

Loader { // this component performs deferred loading.
    id: mainLoader
    onStatusChanged: {
        if( mainLoader.status == Loader.Ready )
        {
            // main page is loaded
            // time to hide splash
            splash.canFinish = true
        }
    }
}

Component.onCompleted:  {
    // splash is already rendered on the screen
    // user is looking on splash
    // now we can start loader to load main page
    mainLoader.source = "main.qml"
}
   }

和SplashScreen.qml:

   import QtQuick 1.1
   import com.nokia.symbian 1.1

   Rectangle {
       id: splash

       anchors.fill: parent
       color: "black"

property int minTimeout: 3000  // 3s by default.
property string image;            // path to splash image
property bool show: false       // if show is true then image opacity is 1.0, else 0.0

property bool canFinish: false    // if true then we can hide spash after timeout

state: show ? "showingSplash" : ""

onStateChanged: {
    if( state == "showingSplash" )
        splashTimer.start();
}

opacity: 0.0

Image {
    source: image
    fillMode: Image.PreserveAspectFit
    anchors.fill: parent
    smooth: true
}

Timer {
    id: splashTimer
    interval: minTimeout
    running: false
    repeat:  true
    onTriggered: {
        if( splash.canFinish )
        {
            // finally we can stop timer and hide splash
            splash.show = false
            splashTimer.repeat = false
        }
        else
        {
            // canFinish is false, but main.qml is not loaded yet
            // we should run timer again and again
            splashTimer.interval = 1000 // 1 sec
            splashTimer.repeat = true
        }
    }
}

states: [
    State {
        name: "showingSplash"
        PropertyChanges { target: splash;  opacity: 1.0 }
    }
]

// hide splash using animation
transitions: [
    Transition {
        from: ""; to: "showingSplash"
        reversible: true
        PropertyAnimation { property: "opacity";  duration: 500; }
    }
]
   }

推荐答案

对我来说,我用一种"老派"的方式来显示没有任何QML的Splash。在这里,您可以看到如何构建它,在渐变的背景上放置图像徽标:

QSplashScreen *Application::buildSplashScreen() {
    const QPixmap logoPixmap(":/images/logo.png");
    QDesktopWidget *desktop = QApplication::desktop();
    QRect desktopRect = desktop->availableGeometry();
    QPixmap splashPixmap(desktopRect.width(), desktopRect.height());
    QPainter painter;
    painter.begin(&splashPixmap);
    QLinearGradient backgroundGradient(splashPixmap.rect().width() / 2,
                                       0,
                                       splashPixmap.rect().width() / 2,
                                       splashPixmap.rect().height());
    backgroundGradient.setColorAt(0, QColor::fromRgb(40, 50, 57));
    backgroundGradient.setColorAt(1, QColor::fromRgb(19, 25, 29));
    painter.fillRect(splashPixmap.rect(), backgroundGradient);
    QRect logoRect((splashPixmap.width() - logoPixmap.width()) / 2,
                   (splashPixmap.height() - logoPixmap.height()) / 2,
                   logoPixmap.width(),
                   logoPixmap.height());
    painter.drawPixmap(logoRect, logoPixmap);
    painter.end();
    QScopedPointer<QSplashScreen> splashScreen(new QSplashScreen(splashPixmap));
    if (desktopRect.width() > desktopRect.height()) {
        splashScreen->setAttribute(Qt::WA_LockLandscapeOrientation, true);
    } else {
        splashScreen->setAttribute(Qt::WA_LockPortraitOrientation, true);
    }
    return splashScreen.take();
}

然后我在程序启动时使用这个手动构建的闪屏:

int Application::run() {
    QScopedPointer<QSplashScreen> splashScreen(buildSplashScreen());
    splashScreen->showFullScreen();
    QScopedPointer<QDeclarativeView> applicationWindow(buildRootView());
    splashScreen->finish(applicationWindow.data());
    applicationWindow->showFullScreen();
    return QApplication::exec();
}

这篇关于QML闪屏无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆