天天看點

Qt程式開機啟動動畫

作者:QT進階進階

多數大型應用程式啟動前都會在程式完全啟動前顯示一個動畫,在程式完全啟動後消失。程式啟動畫面可以顯示相關産品的一些資訊,使使用者在等待程式啟動的同時了解相關産品的功能,這也是一個宣傳的方式。Qt中提供的​QSplashScreen​​類實作了在程式啟動過程中顯示啟動畫面的功能

1、動态啟動畫面

案例一:

1.1、運作結果

Qt程式開機啟動動畫

1.2、具體代碼

#include "mainwindow.h"
#include <QApplication>
#include <QPixmap>
#include <QSplashScreen>
#include <QLabel>
#include <QMovie>
#include <QCoreApplication>
#include <QTime>
#include <QProgressBar>
#include <QDesktopWidget>


void Sleep(int mSeconds)
{
    QTime dieTime = QTime::currentTime().addMSecs(mSeconds);
    while (QTime::currentTime() < dieTime)
    {
        QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
    }
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QDesktopWidget *pDesk = QApplication::desktop();

    QProgressBar progressBar(pDesk);
    progressBar.setRange(0,100);
    progressBar.setWindowFlags(Qt::FramelessWindowHint);//去掉标題欄
    progressBar.resize(700,40);
    progressBar.setTextVisible(false);

    QPixmap pix(":/PowerOn.gif");
    QSplashScreen splash(pix);
    QLabel label(&splash);
    QMovie mv(":/PowerOn.gif");
    label.setMovie(&mv);
    mv.start();

    splash.show();
    progressBar.move(333,610);

    progressBar.show();
    splash.setCursor(Qt::BlankCursor);
    for(int i=0; i<=3000; i+=mv.speed())
    {
        QCoreApplication::processEvents();
        progressBar.setValue(i/30);
        Sleep(mv.speed());
    }

    MainWindow w(pDesk);

    progressBar.close();
    w.move((pDesk->width() - w.width()) / 2, (pDesk->height() - w.height()) / 2);
    w.show();

    splash.finish(&w);

    return a.exec();
}           

注意:滑鼠一點,QSplashScreen畫面就會消失,因為該類内部的滑鼠點選事件預設是hide(),為了實作點選不隐藏,隻需要繼承該類,重寫滑鼠點選事件,函數為空就行,如下所示:

QT開發交流+赀料君羊:714620761
#ifndef
#define

#include <QSplashScreen>
#include <QPixmap>
#include <QMouseEvent>

class MySplashScreent : public QSplashScreen
{
public:
    MySplashScreent(QPixmap pixmap);
    void mousePressEvent(QMouseEvent *) override;
};

#endif// MYSPLASHSCREENT_H           
#include "mysplashscreent.h"

MySplashScreent::MySplashScreent(QPixmap pixmap) : QSplashScreen(pixmap)
{

}
void MySplashScreent::mousePressEvent(QMouseEvent *)
{

}           

案例二:

#include "mainwindow.h"
#include <QApplication>
#include <QSplashScreen>
#include <QElapsedTimer>
#include <QLabel>
#include <QMovie>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    
    QPixmap pixmap(":/images/test.gif");
    QMovie mv(":/images/test.gif");
    QSplashScreen screen(pixmap);
    QLabel label(&screen);
    label.setMovie(&mv);
    mv.start();
    screen.show();
    int delayTime = 5;
    QElapsedTimer timer;
    timer.start();
    while(timer.elapsed() < (delayTime * 1000))
    {
        a.processEvents();
    }

    MainWindow w;
    w.show();
    
    screen.finish(&w);
    
    return a.exec();
}           

2、靜态啟動畫面

2.1、運作結果

啟動畫面

Qt程式開機啟動動畫

主窗體

Qt程式開機啟動動畫

2.2、具體代碼

​​main.cpp​​

#include "mainwindow.h"
#include <QApplication>
#include <QPixmap>
#include <QSplashScreen>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QPixmap pixmap("321.png");
    QSplashScreen splash(pixmap);
    splash.show();
    a.processEvents();  //使程式在顯示啟動動畫的同時仍能響應滑鼠等其它時間
    MainWindow w;
    w.show();
    splash.finish(&w);  //在主窗體對象初始化完成後,結束啟動畫面

    return a.exec();
}           

​構造函數​​

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    setWindowTitle(tr("Splash Example"));
    QTextEdit *edit = new QTextEdit;
    edit->setText("Splash Example");
    setCentralWidget(edit);
    resize(600,450);
    Sleep(1000);  //注(1)
}           

注(1):由于啟動畫面通常在程式初始化時間較長的情況下出現,為了使程式初始化時間加長以顯示啟動畫面,此處調用​​Sleep()​​函數,使主窗體程式在初始化時休眠幾秒。

繼續閱讀