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();
}
相关推荐
用户805533698035 天前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
xcyxiner5 天前
DicomViewer (vcpkg Windows和ubuntu编译)7
qt
Quz10 天前
QML Hello World 入门示例
qt
xcyxiner13 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner13 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner14 天前
DicomViewer (添加模型类)3
qt
xcyxiner15 天前
DicomViewer (目录调整) 2
qt
xcyxiner15 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
LDR00616 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术16 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript