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