Qt启动动画(<QSplashScreen>)

在启动运行qt项目时,加入了启动的动画,让项目感觉更好一点,同时用于一些嵌入式设备,加载动画的同时可以进行项目自检,检查设备是否安全正确,没有出现损伤。

运行结果

下图是运行的结果,启动动画加上加载进度条。

实现代码

下面是实现控件的h文件和c文件,注释详细

h文件
复制代码
#ifndef SPLASHSCREEN_H
#define SPLASHSCREEN_H

#include <QSplashScreen>
#include <QLabel>
#include <QProgressBar>
#include <QVBoxLayout>
#include <QPainter>

class SplashScreen : public QSplashScreen
{
    Q_OBJECT

public:
    // 构造函数,接受一个QPixmap作为背景图片,和一个可选的父窗口指针
    SplashScreen(const QPixmap &pixmap, QWidget *parent = nullptr);
    ~SplashScreen(); // 析构函数

public slots:
    void setProgress(int value); // 设置进度条的值
    void setMessage(const QString &message); // 设置消息文本
    void start(int duration); // 启动进度条,接受一个持续时间作为参数

protected:
    void drawContents(QPainter *painter) override; // 重写绘制内容函数

private:
    QLabel *messageLabel; // 用于显示消息的标签
    QProgressBar *progressBar; // 进度条
};

#endif // SPLASHSCREEN_H
C文件
复制代码
#include "mysplashscreen.h"
#include <QApplication>
#include <QScreen>
#include <QStyle>
#include <QPainter>

// 构造函数
SplashScreen::SplashScreen(const QPixmap &pixmap, QWidget *parent)
    : QSplashScreen(pixmap, Qt::WindowStaysOnTopHint | Qt::SplashScreen)  // 调用QSplashScreen的构造函数,设置窗口标志
{
    // 设置布局
    QVBoxLayout *layout = new QVBoxLayout;
    layout->setContentsMargins(10, 10, 10, 10); // 设置内边距
    setLayout(layout); // 设置布局

    // 创建消息标签
    messageLabel = new QLabel(this);
    messageLabel->setAlignment(Qt::AlignLeft | Qt::AlignTop); // 设置对齐方式
    messageLabel->setStyleSheet("color: white;"); // 设置样式表,设置文字颜色为白色
    layout->addWidget(messageLabel); // 将标签添加到布局中

    // 创建进度条
    progressBar = new QProgressBar(this);
    progressBar->setMaximum(100); // 设置进度条的最大值为100
    progressBar->setValue(0); // 设置进度条的初始值为0
    // 设置进度条的样式表,设置颜色
    progressBar->setStyleSheet(
        "QProgressBar {"
        "   border: 2px solid grey;"
        "   border-radius: 5px;"
        "   text-align: center;"
        "}"
        "QProgressBar::chunk {"
        "   background-color: #05B8CC;"
        "   width: 10px;"
        "   margin: 0.5px;"
        "}");
    layout->addWidget(progressBar); // 将进度条添加到布局中

    // 设置启动画面的几何位置和大小
    setGeometry(QStyle::alignedRect(Qt::LeftToRight, Qt::AlignCenter, size(), qApp->primaryScreen()->geometry()));

    // 初始化显示
    show();
}

// 析构函数
SplashScreen::~SplashScreen()
{
}

// 设置进度条的值
void SplashScreen::setProgress(int value)
{
    progressBar->setValue(value); // 设置进度条的值
    repaint(); // 更新显示
}

// 设置消息文本
void SplashScreen::setMessage(const QString &message)
{
    messageLabel->setText(message); // 设置标签的文本
    repaint(); // 更新显示
}

// 启动进度条
void SplashScreen::start(int duration)
{
    for (int i = 0; i < duration; ++i)
    {
        int progress = i * 100 / duration; // 计算进度
        setProgress(progress); // 设置进度
        setMessage(QString("Loading... %1%").arg(progress)); // 设置消息
        QApplication::processEvents(); // 处理事件,保持界面响应
    }
}

// 重写绘制内容函数
void SplashScreen::drawContents(QPainter *painter)
{
    // 调用基类的绘制内容函数
    QSplashScreen::drawContents(painter);
}
main函数调用

通过设置参数,来设置动画启动的时间

复制代码
#include "TcpClientLogin.h"
#include "mysplashscreen.h"
#include <QApplication>
#include <QFile>
#include <QTextStream>

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

    // 创建并显示启动画面
        QPixmap pixmap(":/res/pic/1.png");
        SplashScreen *splash = new SplashScreen(pixmap);

        // 开始启动画面
        splash->start(10000);
    QFile file(":/res/qss/style-4.qss");/*QSS文件所在的路径*/

    file.open(QFile::ReadOnly);
    QTextStream filetext(&file);
    QString stylesheet = filetext.readAll();
    a.setStyleSheet(stylesheet);
    file.close();

    MainWindow w;

    w.show();
    splash->finish(&w);

    delete splash;

    return a.exec();
}
相关推荐
蜗牛互联网3 小时前
Python Agent 工具调用:审批令牌、参数白名单和累计预算
java·开发语言·人工智能·windows·后端·python
承渊政道4 小时前
【Python编程—从入门到实践】(让程序记得、恢复并解释失败:Python 文件、异常与JSON系统教程)
开发语言·python·pycharm·json·异常处理
一水鉴天4 小时前
声调韵的序:从古汉语音韵到系统科学语言的场效应推演 20261013(千问)
开发语言·人工智能
郭庆汝4 小时前
(六)MCP+A2A之——Playwright与BrowserUse:让AI学会像人类一样使用浏览器
开发语言·python
涉密IT资质笔记4 小时前
涉密资质投入产出分析模型:时间成本、四块成本构成与 ROI 决策框架
java·开发语言·算法
北风toto4 小时前
KMP 算法详解:从暴力匹配到 O(n+m) 的字符串搜索
java·开发语言·算法
william_yangshun4 小时前
WordCraft 观察:纯 Rust 重写文字处理器,把文档变成可编程对象
开发语言·后端·rust·文档自动化
dtq04245 小时前
C++ - 变量
开发语言·c++
撸串-研究生5 小时前
面向景区的综合服务管理平台设计——以稻城亚丁景区为例【附源码】
java·开发语言·vue.js·前后端分离·稻城亚丁景区
wuyk5555 小时前
样章-第03讲-FreeRTOS在16KB-RAM上的裁剪与陷阱
c语言·开发语言·stm32·单片机·嵌入式硬件·物联网·系统架构