找回密码
 立即注册
Qt开源社区 门户 查看内容

Qt实现程序启动动画

2019-7-15 16:53| 发布者: admin| 查看: 2131| 评论: 0

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

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

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

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

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

 main.cpp

  1. #include<QApplication>

  2. #include<QSplashScreen>



  3. int main(int argc,char*argv[])

  4. {

  5. QApplication a(argc, argv);


  6. //创建启动动画类实例,使用资源文件splash.jpg作为展示图片

  7. QSplashScreen splash(QPixmap(":/splash.jpg"));

  8. splash.show();


  9. QWidget w;

  10. w.show();


  11. splash.finish(&w);


  12. return a.exec();

  13. }

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

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

main.cpp

  1. #include<QApplication>

  2. #include<QSplashScreen>


  3. classDataBase

  4. {

  5. public:

  6. void readData()

  7. {

  8. for(int i =0; i <100000;++i)

  9. {

  10. qDebug("reading data");

  11. }

  12. }

  13. };


  14. int main(int argc,char*argv[])

  15. {

  16. QApplication a(argc, argv);


  17. QSplashScreen splash(QPixmap(":/splash.jpg"));

  18. splash.show();


  19. DataBase db;

  20. db.readData();


  21. QWidget w;

  22. w.show();


  23. splash.finish(&w);


  24. return a.exec();

  25. }

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

 splashscreen.h

  1. #ifndef SPLASHSCREEN_H

  2. #define SPLASHSCREEN_H


  3. #include<QSplashScreen>

  4. #include<QProgressBar>


  5. namespaceUi{

  6. classSplashScreen;

  7. }


  8. classSplashScreen:publicQSplashScreen

  9. {

  10. Q_OBJECT


  11. public:

  12. explicitSplashScreen(QPixmap pixmap,QWidget*parent =0);

  13. ~SplashScreen();


  14. //设置进度区间

  15. void setRange(int min,int max);


  16. public slots:

  17. //更新进度

  18. void updateProgress(int num);

  19. void showProgressMessage(int total,constQString& msg);


  20. private:

  21. Ui::SplashScreen*ui;

  22. QProgressBar*bar;//进度条

  23. };


  24. #endif// SPLASHSCREEN_H

splashscreen.cpp

  1. #include<QProgressBar>

  2. #include"splashscreen.h"

  3. #include"ui_splashscreen.h"


  4. SplashScreen::SplashScreen(QPixmap pixmap,QWidget*parent):

  5. QSplashScreen(parent, pixmap),

  6. ui(newUi::SplashScreen)

  7. {

  8. ui->setupUi(this);


  9. bar =newQProgressBar(this);

  10. //设置进度条的位置

  11. bar->setGeometry(0,pixmap.height()-50,pixmap.width(),30);

  12. resize(pixmap.size());

  13. }


  14. SplashScreen::~SplashScreen()

  15. {

  16. delete ui;

  17. }


  18. voidSplashScreen::setRange(int min,int max)

  19. {

  20. bar->setRange(min, max);

  21. }


  22. voidSplashScreen::updateProgress(int num)

  23. {

  24. bar->setValue(num);

  25. }


  26. voidSplashScreen::showProgressMessage(int total,constQString&msg)

  27. {

  28. bar->setRange(0, total);

  29. showMessage(msg);

  30. }

database.h

  1. #ifndef DATABASE_H

  2. #define DATABASE_H


  3. #include<QObject>

  4. #include<QColor>


  5. classDataBase:publicQObject

  6. {

  7. Q_OBJECT

  8. public:

  9. explicitDataBase(QObject*parent =0);

  10. void readData();


  11. signals:

  12. void readingData(int num);

  13. void startReadData(int total,constQString& msg);

  14. };


  15. #endif// DATABASE_H

database.cpp

  1. #include"database.h"


  2. DataBase::DataBase(QObject*parent):QObject(parent)

  3. {


  4. }


  5. voidDataBase::readData()

  6. {

  7. int max =10000;

  8. emit startReadData(max,"is reading data");

  9. for(int i =0; i < max;++i)

  10. {

  11. emit this->readingData(i);

  12. qDebug("reading data");

  13. }

  14. }

main.cpp

  1. #include<QApplication>

  2. #include"splashscreen.h"

  3. #include"database.h"



  4. int main(int argc,char*argv[])

  5. {

  6. QApplication a(argc, argv);


  7. SplashScreen splash(QPixmap(":/splash.jpg"));

  8. splash.show();

  9. a.processEvents();


  10. DataBase db;

  11. QObject::connect(&db, SIGNAL(startReadData(int,QString)),

  12. &splash, SLOT(showProgressMessage(int,QString)));

  13. QObject::connect(&db, SIGNAL(readingData(int)),

  14. &splash, SLOT(updateProgress(int)));

  15. db.readData();


  16. QWidget w;

  17. w.show();


  18. splash.finish(&w);


  19. return a.exec();

  20. }

运行效果:



好了,关于启动动画就先分享到这里啦,喜欢的就点个赞吧。

最近,有很多小伙伴问我有没有关于初学Qt的书,关于Qt的书真不是很多,但下面这本Qt Creator快速入门是一本初学者必备的好书,我入门也是买的这本书,简单易懂,看完之后相信你就对Qt有了比较全面的认识了。有需要的可以点击购买哦。


----------------------------------------------------------------------------------------------------------------------
我们尊重原创,也注重分享,文章来源于微信公众号:小豆君Qt分享,建议关注公众号查看原文。如若侵权请联系qter@qter.org。
----------------------------------------------------------------------------------------------------------------------

鲜花

握手

雷人

路过

鸡蛋

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了