一、Qt有3种方式实现程序启动动画(介绍)
1、QSplashScreen 静态图片(png、jpg等格式)
2、QMovie 动态图片(gif格式)
3、QAxWidget 视频(swf格式)
1.QSplashScreen 静态图片(png、jpg等格式)
//创建启动动画类实例
QSplashScreen splash(QPixmap("C:/Users/10600/Pictures/imgs/p1.png")); //文件绝对路径 也可以使用相对路径
splash.showMessage(QStringLiteral("正在初始化..."));//消息提示
splash.show(); //显示a.processEvents(); //保证先完成启动画面的绘制显示,再执行后面的w显示//主界面显示
MainWindow w;
w.show();splash.finish(&w); //结束
2.QMovie 动态图片(gif格式)
QMovie movie("C:/Users/10600/Pictures/tai.gif");
QLabel label;label.setGeometry(300, 300, 500, 500);
label.setMovie(&movie);
label.setScaledContents(true); //自动适应窗口大小
label.setWindowFlags(Qt::FramelessWindowHint); //去除边框movie.start();
label.show();//延迟5秒
QTime t;
t.start();
while(t.elapsed() < 5000)
{
QApplication::processEvents();
}//主界面显示
MainWindow w;
w.show();
label.close();
3.QAxWidget视频(swf格式)
需要在.pro文件中添加 QT += axcontainer
QTextCodec *codec = QTextCodec::codecForName("GB2312"); //文本为GB2312编码
QTextCodec::setCodecForLocale(codec); //设置本地编码QAxWidget flash;
flash.resize(800,600); //设置该控件的初始大小
flash.setControl(QString::fromUtf8("{d27cdb6e-ae6d-11cf-96b8-444553540000}")); //设定控制器
flash.dynamicCall("LoadMovie(long,string)", 0, "C:/Users/10600/Videos/2.swf"); //文件绝对路径 也可以使用相对路径
flash.setWindowFlags(Qt::FramelessWindowHint); //去除边框
flash.show();//延迟5秒
QTime t;
t.start();
while(t.elapsed() < 5000)
{
QApplication::processEvents();
}//主界面显示
MainWindow w;
w.show();
flash.close();
二、QT 实现软件启动动画与加载进度(实现)
下面是一个完整的 QT 实现方案,包含启动动画、主界面加载和进度条显示功能。
功能说明
实现启动动画,在等待主界面出现之前加载动画,主界面加载完成后动画结束,显示主界面,启动动画是一个图片加从一个点到四个点的加载动画
代码
1. 自定义启动画面类 (animatedsplash.h)
#ifndef ANIMATEDSPLASH_H
#define ANIMATEDSPLASH_H#include <QSplashScreen>
#include <QTimer>
#include <QPainter>class AnimatedSplash : public QSplashScreen
{Q_OBJECT
public:explicit AnimatedSplash(const QPixmap &background, QWidget *parent = nullptr);~AnimatedSplash();void setLoadingDotsColor(const QColor &color);void setLoadingDotsRadius(int radius);void setLoadingDotsSpacing(int spacing);protected:void drawContents(QPainter *painter) override;private slots:void updateAnimation();private:QTimer *m_animationTimer;int m_dotPosition; // 当前动画位置 (0-3)QColor m_dotsColor;int m_dotsRadius;int m_dotsSpacing;QPixmap m_background;
};#endif // ANIMATEDSPLASH_H
2. 自定义启动画面实现 (animatedsplash.cpp)
#include "AnimatedSplash.h"
#include <QApplication>
#include <QPainter>AnimatedSplash::AnimatedSplash(const QPixmap &background, QWidget *parent): QSplashScreen(background),m_dotPosition(0),m_dotsColor(Qt::white),m_dotsRadius(8),m_dotsSpacing(20),m_background(background)
{// 设置动画定时器m_animationTimer = new QTimer(this);connect(m_animationTimer, &QTimer::timeout, this, &AnimatedSplash::updateAnimation);m_animationTimer->start(300); // 每300ms更新一次动画// 设置窗口属性setFixedSize(background.size());setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
}AnimatedSplash::~AnimatedSplash()
{m_animationTimer->stop();delete m_animationTimer;
}void AnimatedSplash::setLoadingDotsColor(const QColor &color)
{m_dotsColor = color;
}void AnimatedSplash::setLoadingDotsRadius(int radius)
{m_dotsRadius = radius;
}void AnimatedSplash::setLoadingDotsSpacing(int spacing)
{m_dotsSpacing = spacing;
}void AnimatedSplash::drawContents(QPainter *painter)
{// 先绘制背景图QSplashScreen::drawContents(painter);// 计算加载点的中心Y位置(距离底部50像素)int centerY = height() - 50;int startX = width() / 2 - (m_dotsRadius * 2 + m_dotsSpacing * 1.5);// 绘制"启动中"文字painter->setPen(m_dotsColor); // 使用相同的颜色painter->setOpacity(1.0); // 完全不透明QFont font = painter->font();font.setPixelSize(m_dotsRadius * 6); // 设置合适的字体大小painter->setFont(font);// 计算文字位置(在第一个点左侧留出空间)int textWidth = painter->fontMetrics().horizontalAdvance("启动中");int textX = startX - textWidth - m_dotsSpacing; // 在第一个点左侧留出一个间距painter->drawText(textX, centerY + m_dotsRadius, "启动中");// 绘制5个点for (int i = 0; i < 5; ++i) {// 当前点是否应该高亮(根据动画位置)bool isActive = (i == m_dotPosition);qreal opacity = isActive ? 1.0 : 0.3;int radius = isActive ? m_dotsRadius : m_dotsRadius * 0.8;painter->setPen(Qt::NoPen);painter->setBrush(m_dotsColor);painter->setOpacity(opacity);int x = startX + i * (m_dotsRadius * 2 + m_dotsSpacing);painter->drawEllipse(QPoint(x, centerY), radius, radius);}// 重置画笔设置painter->setOpacity(1.0);
}
void AnimatedSplash::updateAnimation()
{m_dotPosition = (m_dotPosition + 1) % 4;update(); // 触发重绘
}
3. 主程序入口 (main.cpp)
#include "lectotype/lectotype.h"
#include "MainWindow/functionWindow.h"
#include <QApplication>
#include <QStyleFactory>
#include <QThread>
#include "CustomSplash/animatedsplash.h"int main(int argc, char *argv[])
{QApplication a(argc, argv);// Lectotype w;// w.show();// 设置应用程序样式//QApplication::setStyle(QStyleFactory::create("Fusion"));// 1. 创建并显示自定义启动画面QPixmap splashPix("F:/DELTA/DELTA/bin/res/Functionwindow/alarm.png"); // 启动背景图AnimatedSplash splash(splashPix);// 自定义加载点样式splash.setLoadingDotsColor(QColor(255, 215, 0)); // 金色splash.setLoadingDotsRadius(6);splash.setLoadingDotsSpacing(15);splash.show();a.processEvents(); // 确保界面能立即更新// 2. 在后台创建并加载主窗口FunctionWindow *w=new FunctionWindow();// 3. 加载完成后显示主窗口,关闭启动画面splash.finish(w);w->show();return a.exec();
}