qt设置开机启动动画_Qt实现程序启动动画

本文介绍如何使用Qt的QSplashScreen类创建启动动画,通过模拟读取数据库数据延长启动时间,并添加进度条显示程序启动状态。示例代码展示了从启动动画到主窗口显示的完整过程。
摘要由CSDN通过智能技术生成

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

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

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

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

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

main.cpp

#include #include

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 #include

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 #include

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 #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 #include

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 #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分享

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值