Qt程序开机启动动画

多数大型应用程序启动前都会在程序完全启动前显示一个动画,在程序完全启动后消失。程序启动画面可以显示相关产品的一些信息,使用户在等待程序启动的同时了解相关产品的功能,这也是一个宣传的方式。Qt中提供的​QSplashScreen​​类实现了在程序启动过程中显示启动画面的功能

1、动态启动画面

案例一:

1.1、运行结果

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、运行结果

启动画面

主窗体

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()​​函数,使主窗体程序在初始化时休眠几秒。

举报
评论 0