Qt设置软件启动动画(开屏动画、欢迎界面)

启动动画的相关接口为QSplashScreen。

版本1:显示图片+延时

代码很简单,只需要在main.cpp中添加几行代码即可实现:

#include "pclvisualizer.h"

#include 
#include  //添加QDateTime头文件
#include 
#include 
int
main(int argc, char* argv[])
{
  QApplication a(argc, argv);

  QPixmap pixmap("Qt.png");     //读取图片
  QSplashScreen splash(pixmap); //
  splash.setWindowOpacity(0.8); // 设置窗口透明度
  splash.show();
  splash.showMessage("程序正在加载......", Qt::AlignCenter, Qt::red); //显示文字
  QDateTime time = QDateTime::currentDateTime();
  QDateTime currentTime = QDateTime::currentDateTime(); //记录当前时间
  while (time.secsTo(currentTime) <= 5) // 5为需要延时的秒数
  {
    currentTime = QDateTime::currentDateTime();
    a.processEvents();
  };

  PCLVisualizer w;
  w.show();

  splash.finish(&w); //在主体对象初始化完成后结束启动动画

  return a.exec();
}

效果如下图:

Qt设置软件启动动画(开屏动画、欢迎界面)_第1张图片

 版本2:显示gif动态图片+延时

#include 
#include 
#include 
#include 

int
main(int argc, char* argv[])
{
  QApplication a(argc, argv);

  QSplashScreen splash(pixmap); //
  splash.setWindowOpacity(0.8); // 设置窗口透明度
  QLabel label(&splash);
  QMovie mv("G.gif");
  label.setMovie(&mv);
  mv.start();
  //显示此启动图片
  splash.show();
  splash.setCursor(Qt::BlankCursor);
  for (int i = 0; i < 3000; i += mv.speed()) {
    a.processEvents(); //使程序在显示启动画面的同时仍能响应鼠标等其他事件
    Sleep(mv.speed()); // 延时
  }

  PCLVisualizer w;
  w.show();

  splash.finish(&w); //在主体对象初始化完成后结束启动动画

  return a.exec();
}

参考文章:

Qt实现程序启动动画_晓星-CSDN博客_qt启动动画演示一个应用程序启动时,添加启动动画的小例子。所谓启动动画,就是说当一个应用程序启动时,在展示主窗口之前,有可能会先去初始化一些运行环境,验证用户信息等前提工作。那么在这段空闲期程序的启动过程是没有用户界面的,而用户也无法得知程序的状态,所以就需要在这段空白时间中,向用户提供一个展示程序运行状态的窗口,来为用户提供积极的正反馈。启动动画在很多软件中得到了应用,例如游戏加载画面,VS的启动画面等...https://blog.csdn.net/a849473785/article/details/95939641

QT 程序启动动画_Sakuya__的博客-CSDN博客_qt启动动画简述我们日常用的很多软件都有启动动画,比如Visual Studio和PyCharm在打开软件之前都会有一个加载各种组件的过程。它们的启动动画就是告诉你程序正在打开的过程中,正在加载组件,而不是让你以为程序没有启动。那么,QT中可不可以实现这样的效果呢,当然是可以的。QT提供了QSplashScreen这个类来实现启动动画的效果。效果代码之路最基本的...https://blog.csdn.net/sakuya__/article/details/88973724

你可能感兴趣的:(Qt,qt,c++)