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

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

版本1:显示图片+延时

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

#include "pclvisualizer.h"

#include <QApplication>
#include <QDateTime> //添加QDateTime头文件
#include <QPixmap>
#include <QSplashScreen>
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();
}

效果如下图:

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

#include <QApplication>
#include <QMovie>
#include <QPixmap>
#include <QSplashScreen>

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();
}
经验分享 程序员 微信小程序 职场和发展