Qt实现程序启动动画

简介: Qt实现程序启动动画

Hello,大家好,好久没有更新文章了,有人问我,“为什么还没有更新文章,我们都还在等着呢”,我也不知道该如何回答,可能是我太懒了吧,曾有段时间想要放弃,但是男人总不能轻言放弃,说过的话必须兑现,还有那么多可爱的小伙伴支持我,所以从今天开始继续为大家分享Qt。

这次我们来演示一个应用程序启动时,添加启动动画的小例子。

所谓启动动画,就是说当一个应用程序启动时,在展示主窗口之前,有可能会先去初始化一些运行环境,验证用户信息等前提工作。那么在这段空闲期程序的启动过程是没有用户界面的,而用户也无法得知程序的状态,所以就需要在这段空白时间中,向用户提供一个展示程序运行状态的窗口,来为用户提供积极的正反馈。

启动动画在很多软件中得到了应用,例如游戏加载画面,VS的启动画面等。

当然,强大的Qt也为我们提供了启动动画的相关接口,即QSplashScreen,下面代码是实现启动动画的一个简单例子:

main.cpp

#include <QApplication>
#include <QSplashScreen>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    //创建启动动画类实例,使用资源文件splash.jpg作为展示图片
    QSplashScreen splash(QPixmap(":/splash.jpg"));
    splash.show();
    QWidget w;
    w.show();
    splash.finish(&w);
    return a.exec();
}


然后运行程序,出现启动动画效果,然后出现主窗口。

但此时动画一闪而过,那是因为程序什么都没有做,为此再模拟一个读取数据库数据的代码,以加长启动时间。

main.cpp

#include <QApplication>
#include <QSplashScreen>
class DataBase
{
public:
    void readData()
    {
        for (int i = 0; i < 100000; ++i)
        {
            qDebug("reading data");
        }
    }
};
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QSplashScreen splash(QPixmap(":/splash.jpg"));
    splash.show();
    DataBase db;
    db.readData();
    QWidget w;
    w.show();
    splash.finish(&w);
    return a.exec();
}


在正常情况下,仅仅提供一张图片对用户其实是不友善的,所以我们还可以添加一个进度条来标识应用程序的启动状态。添加一个SplashScreen类

splashscreen.h

#ifndef SPLASHSCREEN_H
#define SPLASHSCREEN_H
#include <QSplashScreen>
#include <QProgressBar>
namespace Ui {
class SplashScreen;
}
class SplashScreen : public QSplashScreen
{
    Q_OBJECT
public:
    explicit SplashScreen(QPixmap pixmap, QWidget *parent = 0);
    ~SplashScreen();
    //设置进度区间
    void setRange(int min, int max);
public slots:
    //更新进度
    void updateProgress(int num);
    void showProgressMessage(int total, const QString& msg);
private:
    Ui::SplashScreen *ui;
    QProgressBar *bar; //进度条
};
#endif // SPLASHSCREEN_H


splashscreen.cpp

#include <QProgressBar>
#include "splashscreen.h"
#include "ui_splashscreen.h"
SplashScreen::SplashScreen(QPixmap pixmap, QWidget *parent) :
    QSplashScreen(parent, pixmap),
    ui(new Ui::SplashScreen)
{
    ui->setupUi(this);
    bar = new QProgressBar(this);
    //设置进度条的位置
    bar->setGeometry(0,pixmap.height()-50,pixmap.width(),30);
    resize(pixmap.size());
}
SplashScreen::~SplashScreen()
{
    delete ui;
}
void SplashScreen::setRange(int min, int max)
{
    bar->setRange(min, max);
}
void SplashScreen::updateProgress(int num)
{
    bar->setValue(num);
}
void SplashScreen::showProgressMessage(int total, const QString &msg)
{
    bar->setRange(0, total);
    showMessage(msg);
}


database.h

#ifndef DATABASE_H
#define DATABASE_H
#include <QObject>
#include <QColor>
class DataBase : public QObject
{
    Q_OBJECT
public:
    explicit DataBase(QObject *parent = 0);
    void readData();
signals:
    void readingData(int num);
    void startReadData(int total, const QString& msg);
};
#endif // DATABASE_H


database.cpp

#include "database.h"
DataBase::DataBase(QObject *parent) : QObject(parent)
{
}
void DataBase::readData()
{
    int max = 10000;
    emit startReadData(max, "is reading data");
    for (int i = 0; i < max; ++i)
    {
        emit this->readingData(i);
        qDebug("reading data");
    }
}


main.cpp

#include <QApplication>
#include "splashscreen.h"
#include "database.h"
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    SplashScreen splash(QPixmap(":/splash.jpg"));
    splash.show();
    a.processEvents();
    DataBase db;
    QObject::connect(&db, SIGNAL(startReadData(int, QString)),
                     &splash, SLOT(showProgressMessage(int,QString)));
    QObject::connect(&db, SIGNAL(readingData(int)),
                     &splash, SLOT(updateProgress(int)));
    db.readData();
    QWidget w;
    w.show();
    splash.finish(&w);
    return a.exec();
}


运行效果:


好了,这次的分享就到这里,我们下次再见,最后不要忘记点赞和分享哦,您的支持就是对原创,分享的最大鼓励。


欢迎关注微信公众号-小豆君Qt分享

相关文章
|
2月前
|
Web App开发 存储 Linux
Linux(33)Rockchip RK3568 Ubuntu22.04上通过SSH运行Qt程序和关闭Chrome的密钥提示
Linux(33)Rockchip RK3568 Ubuntu22.04上通过SSH运行Qt程序和关闭Chrome的密钥提示
164 0
|
2月前
【QT】读写.ini配置文件的程序实现
【QT】读写.ini配置文件的程序实现
|
8月前
|
存储 Cloud Native Linux
C++Qt防止程序重复启动
C++Qt防止程序重复启动
|
10天前
|
Linux iOS开发 开发者
Qt问题(二):无法定位程序输入点于动态链接库
动态链接库(Dynamic Link Library,简称DLL)是一种可执行文件格式,常见于Windows操作系统中,而在Linux和macOS等其他操作系统中,相似的概念通常被称为共享库(Shared Library)。动态链接库允许程序在运行时加载所需的代码和数据,而不是在编译时静态链接到应用程序中。这种方式带来了几个重要的优点:
|
2月前
|
开发框架 自然语言处理 Linux
Qt:构建强大跨平台应用程序的框架
Qt:构建强大跨平台应用程序的框架
|
10天前
|
调度
【浅入浅出】Qt多线程机制解析:提升程序响应性与并发处理能力
在学习QT线程的时候我们首先要知道的是QT的主线程,也叫GUI线程,意如其名,也就是我们程序的最主要的一个线程,主要负责初始化界面并监听事件循环,并根据事件处理做出界面上的反馈。但是当我们只限于在一个主线程上书写逻辑时碰到了需要一直等待的事件该怎么办?它的加载必定会带着主界面的卡顿,这时候我们就要去使用多线程。
|
1月前
Qt MainWindow 程序主窗口
Qt MainWindow 程序主窗口
|
2月前
|
C++
QT第一个程序命名空间详解,解释ui_widget的和xxx.cpp的联系
QT第一个程序命名空间详解,解释ui_widget的和xxx.cpp的联系
54 0
|
2月前
|
编译器
QT creator开发环境下 界面更改后运行程序不能实时更新或者在源文件添加该控件后无法编译的问题
在使用QT Creator开发界面的过程中,偶尔会出现添加控件后,运行程序后,界面控件无法更新的情况,或者在源文件使用该控件却出现无法编译的情况,使用QT Creator 4.8.2也会出现这个情况,也不知道这种情况会不会在以后有所改善。
97 0
|
2月前
|
人工智能 编译器 C++
新版Qt6快速打包程序脚本
不知道啥时候Qt更新了,目前是6.7.0,项目默认的生成路径改了, 从 项目目录的同级目录 改为了 项目目录中的build目录, 之前的脚本也用不了了,所以用AI更新了一下脚本, 希望能帮到大家,方便快速打包程序到桌面。
65 9