QSplashScreen --软件启动前的交互

server/2025/2/26 10:58:10/

目录

QSplashScreen 类介绍

 使用方式

项目中使用

THPrinterSplashScreen头文件

 THPrinterSplashScreen实现代码

使用代码 

使用效果

 


QSplashScreen 类介绍

         QSplashScreen 是 Qt 中的一个类,用于显示启动画面。它通常在应用程序启动时显示,以向用户显示应用程序正在启动的状态。启动画面可以是一个图片,也可以是一个包含了文本、图片等内容的窗口。

QSplashScreen(const QPixmap &pixmap = QPixmap(), Qt::WindowFlags f = Qt::WindowFlags())

QSplashScreen(QWidget *parent, const QPixmap &pixmap = QPixmap(), Qt::WindowFlags f = Qt::WindowFlags())


virtual ~QSplashScreen()


void finish(QWidget *mainWin)

QString message() const


const QPixmap pixmap() const


void repaint()


void  setPixmap(const QPixmap &pixmap)

//slots
void clearMessage()
void showMessage(const QString &message, int alignment = Qt::AlignLeft, const QColor &color = Qt::black)

//protected 可以继承自绘
virtual void drawContents(QPainter *painter)
 

 使用方式

        以下是Qt官方文档给出的两种使用场景。

  作为主窗口启动前的启动动画

  int main(int argc, char *argv[]){QApplication app(argc, argv);QPixmap pixmap(":/splash.png");QSplashScreen splash(pixmap);splash.show();app.processEvents();...QMainWindow window;window.show();splash.finish(&window);return app.exec();}

主窗口启动前软件启动提示信息

  QPixmap pixmap(":/splash.png");QSplashScreen *splash = new QSplashScreen(pixmap);splash->show();... // Loading some itemssplash->showMessage("Loaded modules");qApp->processEvents();... // Establishing connectionssplash->showMessage("Established connections");qApp->processEvents();

项目中使用

        实际项目中如果软件启动比较耗时,一般需要根据软件的样式风格和互动需求自定义启动动画效果,此时virtual void drawContents(QPainter *painter) 和 repaint()就显得尤为重要。

        以下是根据自身项目,加载启动动画时显示软件版本信息和启动进度等信息,主要继承drawContents进行重绘。

THPrinterSplashScreen头文件

#ifndef THPrinterSplashScreenT_H
#define THPrinterSplashScreenT_H#include <QSplashScreen>
#include "Common.h"#define g_pSplashScreen Singleton<THPrinterSplashScreen>::getInstance()class THPrinterSplashScreen : public QSplashScreen
{Q_OBJECTfriend Singleton<THPrinterSplashScreen>;
public://关闭自身前可以再次操作void finish(QWidget *w);//设置启动进度0-100void setProgressValue(int value);//设置启动提示信息 如库加载信息、数据库启动...void setTipStr(const QString&tipStr);protected://重写此函数 自定义绘制启动动画void drawContents(QPainter *painter) override;
private:THPrinterSplashScreen();~THPrinterSplashScreen() = default;QPixmap m_pixIcon;QPixmap m_picBackground;int     m_nProgressValue = 0;QString m_strTip;
};#endif // THPrinterSplashScreenT_H

 THPrinterSplashScreen实现代码


#pragma execution_character_set("utf-8")THPrinterSplashScreen::THPrinterSplashScreen()	
{m_picBackground.load(":/images/icon/background.png");m_pixIcon.load(":/images/icon/logo.png");setPixmap(m_picBackground);setWindowFlag(Qt::WindowStaysOnTopHint);
}void THPrinterSplashScreen::finish(QWidget *w)
{setProgressValue(100);setTipStr("程序加载完成!");QSplashScreen::finish(w);
}void THPrinterSplashScreen::setProgressValue(int value)
{if (isVisible() && value >= 0 && m_nProgressValue < value) {value = qBound(0, value,100);m_nProgressValue = value;repaint();}
}void THPrinterSplashScreen::setTipStr(const QString&tipStr)
{if (isVisible() && !tipStr.isEmpty() && m_strTip != tipStr) {m_strTip = tipStr;repaint();}
}void THPrinterSplashScreen::drawContents(QPainter *painter)
{QSplashScreen::drawContents(painter);int bg_w = m_picBackground.width();int bg_h = m_picBackground.height();int icon_w = m_pixIcon.width();int icon_h = m_pixIcon.height();//默认垂直方向dpi为96 防止不同设备分辨率不同字体差异过大float fFactor = logicalDpiY() / 96.0f; int smallFontSize = qRound(10 * fFactor);int midFontSize = qRound(15 * fFactor);int bigFontSize = qRound(20 * fFactor);int fontGapSize = 6;int magrinGapSize = 10;int offset = -20;int icon_x = (bg_w - icon_w) / 2;int icon_y = (bg_h - icon_h) / 2  + offset;int text_name_y = (bg_h + icon_h) / 2 + magrinGapSize + offset;int text_TipStr_y = text_name_y + bigFontSize + fontGapSize;int text_version_y = bg_h - fontGapSize - midFontSize;QRect rect_Icon(icon_x, icon_y, icon_w, icon_h);//相对于parent 左上角坐标 长宽QRect rect_Name_Text(0, text_name_y, bg_w, bigFontSize + fontGapSize);QRect rect_TipStr_Text(0, text_TipStr_y, bg_w, smallFontSize + fontGapSize);QRect rect_Version_Text(0, text_version_y, bg_w, midFontSize + fontGapSize);// 绘制启动动画logopainter->drawPixmap(rect_Icon, m_pixIcon);//绘制软件名称auto font = painter->font();font.setBold(true);font.setPointSize(bigFontSize);painter->setFont(font);auto pen = painter->pen();pen.setColor(Qt::white);painter->setPen(pen);painter->drawText(rect_Name_Text, Qt::AlignCenter, tr("设备指纹烧录工具"));//绘制启动中提示信息font = painter->font();font.setBold(false);font.setPointSize(smallFontSize);painter->setFont(font);if (!m_strTip.isEmpty()){painter->drawText(rect_TipStr_Text, Qt::AlignCenter, m_strTip);}//绘制软件版本信息font = painter->font();font.setPointSize(midFontSize);painter->setFont(font);auto &strVersion = PmsUpDater::getVersion();if (!strVersion.isEmpty()) {painter->drawText(rect_Version_Text, Qt::AlignCenter, strVersion);}//在rect_Version_Text最右侧绘制软件启动进度if (m_nProgressValue >= 0) {rect_Version_Text.adjust(0, 0, -midFontSize, 0);painter->drawText(rect_Version_Text,Qt::AlignVCenter | Qt::AlignRight,QString("%1%").arg(m_nProgressValue));}
}

使用代码 

main函数中嵌入到软件主界面启动前后。

	int main(int argc, char *argv[]){//...g_pSplashScreen->setProgressValue(0);g_pSplashScreen->show();PmsUpDater w;w.show();g_pSplashScreen->finish(&w);//...return a.exec();}

        在程序启动比较耗时的地方添加进度信息和提示信息,便于判断程序启动的状态,若程序启动失败也可作为定位失败位置的信息。

int THPrinter::Initial(){//...//初始化SDKInitialSdk();g_pSplashScreen->setTipStr("SDK初始化成功!");g_pSplashScreen->setProgressValue(53);//...//数据库连接开始g_pSplashScreen->setTipStr("数据库连接中...");g_pSplashScreen->setProgressValue(56);//...//连接完成g_pSplashScreen->setTipStr("数据库连完成");g_pSplashScreen->setProgressValue(57);//...}

使用效果


http://www.ppmy.cn/server/170722.html

相关文章

Golang学习笔记_38——享元模式

Golang学习笔记_35——代理模式 Golang学习笔记_36——装饰器模式 Golang学习笔记_37——外观模式 文章目录 享元模式&#xff08;Flyweight Pattern&#xff09;详解一、核心概念1. 定义2. 解决的问题3. 核心角色4. 类图 二、特点分析三、适用场景1. 文字编辑器2. 游戏开发3. …

嵌入式项目:STM32刷卡指纹智能门禁系统

本文详细介绍基于STM32的刷卡指纹智能门禁系统。 获取资料/指导答疑/技术交流/选题/帮助&#xff0c;请点链接&#xff1a; https://gitee.com/zengzhaorong/share_contact/blob/master/stm32.txt 1 系统功能 1.1 功能概述 本系统由STM32硬件端&#xff08;下位机&#xff09;…

Spring Boot中@EnableAutoConfiguration的魔法与实例解析

在Spring Boot的世界里&#xff0c;EnableAutoConfiguration注解扮演着一个极为重要的角色。它不仅简化了Spring应用的配置过程&#xff0c;还通过智能的自动配置机制&#xff0c;让开发者能够更加专注于业务逻辑的实现&#xff0c;而无需过多地操心底层的配置细节。今天&#…

力扣 下一个排列

交换位置&#xff0c;双指针&#xff0c;排序。 题目 下一个排列即在组成的排列中的下一个大的数&#xff0c;然后当这个排列为降序时即这个排列最大&#xff0c;因为大的数在前面&#xff0c;降序排列的下一个数即升序。所以&#xff0c;要是想找到当前排列的下一个排列&…

算法与数据结构(格雷编码)

题目 思路 首先我们先看一下格雷编码的一些情况&#xff0c;为了一会方便理解&#xff0c;我们看它的二进制情况。 当n1时&#xff0c;输出[0&#xff0c;1] 当n2时&#xff0c;输出[00,01,11,10] 当n3时&#xff0c;输出[000, 001, 011, 010, 110, 111, 101, 100] 我们可…

lua基础语法学习

lua基础语法学习 文章目录 lua基础语法学习1. 基础2. 输入输出3. 分支结构与循环结构4. 函数5. 元表与元方法6. 面向对象 1. 基础 注释 --单行注释--[[ 多行注释 --]]标识符 标识符以一个字母 A 到 Z 或 a 到 z 或下划线 _ 开头后加上 0 个或多个字母&#xff0c;下划线&…

C#中级教程(1)——解锁 C# 编程的调试与错误处理秘籍

一、认识错误&#xff1a;编程路上的 “绊脚石” 在 C# 编程中&#xff0c;错误大致可分为两类&#xff1a;语法错误和语义错误&#xff08;逻辑错误&#xff09;。语法错误就像是写作文时的错别字和病句&#xff0c;编译器一眼就能识别出来&#xff0c;比如变量名拼写错误、符…

【大模型】Ubuntu下 fastgpt 的部署和使用

前言 本次安装的版本为 fastgpt:v4.8.8-fix2。 最新版本fastgpt:v4.8.20-fix2 问答时报错&#xff0c;本着跑通先使用起来&#xff0c;就没有死磕下去&#xff0c;后面bug解了再进行记录。   github连接&#xff1a;https://github.com/labring/FastGPT fastgpt 安装说明&…