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();
}

运行效果:

下载链接

在这里插入图片描述

  • 7
    点赞
  • 70
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
Qt是一个跨平台的C++应用程序开发框架,它提供了丰富的GUI和媒体功能,使开发者能够快速创建各种应用程序。在Qt中,我们可以使用动态进度条和启动画面来增强用户体验。 动态进度条是一个可以显示任务进度的控件,它与任务的完成情况同步更新。我们可以使用Qt中的QProgressBar类来创建和管理动态进度条。在启动画面中,我们可以使用Qt中的QSplashScreen类来显示一个带有应用程序标识和加载信息的启动画面。 要创建动态进度条和启动画面,我们可以按照以下步骤进行: 1. 创建一个QSplashScreen实例,并设置启动画面的背景图片和显示文本。 2. 在应用程序主窗口的构造函数中,创建一个QProgressBar实例,并设置其初始值和范围。 3. 在任务执行过程中,使用QTimer类来模拟任务的进度,通过更新QProgressBar的value属性来更新进度条的显示。 4. 在任务完成后,关闭启动画面并显示应用程序主窗口。 下面是一个使用动态进度条和启动画面的示例代码: ```cpp #include <QtWidgets> int main(int argc, char *argv[]) { QApplication app(argc, argv); QSplashScreen splash(QPixmap(":/splash_image.png")); QProgressBar progressBar; splash.showMessage("Loading...", Qt::AlignBottom|Qt::AlignHCenter, Qt::white); splash.show(); QTime waitTime = QTime::currentTime().addSecs(3); // 模拟加载过程 while (QTime::currentTime() < waitTime) { app.processEvents(); } progressBar.setRange(0, 100); progressBar.setValue(0); progressBar.setAlignment(Qt::AlignCenter); QMainWindow mainWindow; mainWindow.setCentralWidget(&progressBar); mainWindow.show(); QTimer timer; QObject::connect(&timer, &QTimer::timeout, [&progressBar]() mutable { int value = progressBar.value() + 10; if (value <= progressBar.maximum()) { progressBar.setValue(value); } }); timer.start(500); splash.finish(&mainWindow); return app.exec(); } ``` 以上代码中,我们使用了一张启动画面图片,并设置了加载信息。然后,程序会在启动画面显示3秒钟,模拟加载过程。加载完成后,我们创建了应用程序的主窗口,并将QProgressBar设置为其中央部件。通过QTimer来定时更新进度条的值,最后关闭启动画面并显示主窗口。 使用动态进度条和启动画面可以给用户一个友好的界面提示,提升用户体验。在实际的应用程序中,我们可以根据具体需求来设计和实现更丰富和复杂的动态进度条和启动画面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值