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>
classDataBase
{
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>
namespaceUi{
classSplashScreen;
}
classSplashScreen:publicQSplashScreen
{
Q_OBJECT
public:
explicitSplashScreen(QPixmap pixmap,QWidget*parent =0);
~SplashScreen();
//设置进度区间
void setRange(int min,int max);
public slots:
//更新进度
void updateProgress(int num);
void showProgressMessage(int total,constQString& 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(newUi::SplashScreen)
{
ui->setupUi(this);
bar =newQProgressBar(this);
//设置进度条的位置
bar->setGeometry(0,pixmap.height()-50,pixmap.width(),30);
resize(pixmap.size());
}
SplashScreen::~SplashScreen()
{
delete ui;
}
voidSplashScreen::setRange(int min,int max)
{
bar->setRange(min, max);
}
voidSplashScreen::updateProgress(int num)
{
bar->setValue(num);
}
voidSplashScreen::showProgressMessage(int total,constQString&msg)
{
bar->setRange(0, total);
showMessage(msg);
}
database.h
#ifndef DATABASE_H
#define DATABASE_H
#include<QObject>
#include<QColor>
classDataBase:publicQObject
{
Q_OBJECT
public:
explicitDataBase(QObject*parent =0);
void readData();
signals:
void readingData(int num);
void startReadData(int total,constQString& msg);
};
#endif// DATABASE_H
database.cpp
#include"database.h"
DataBase::DataBase(QObject*parent):QObject(parent)
{
}
voidDataBase::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的书,关于Qt的书真不是很多,但下面这本Qt Creator快速入门是一本初学者必备的好书,我入门也是买的这本书,简单易懂,看完之后相信你就对Qt有了比较全面的认识了。有需要的可以点击购买哦。
---------------------------------------------------------------------------------------------------------------------- 我们尊重原创,也注重分享,文章来源于微信公众号:小豆君Qt分享,建议关注公众号查看原文。如若侵权请联系qter@qter.org。 ----------------------------------------------------------------------------------------------------------------------
|