当前位置: 首页 > news >正文

QT全局气泡类型提示框【自动宽度、多屏自适应居中】

参考 qt 消息弹出框 ,无框,缓慢自动消失_new messagetips格式-CSDN博客 进行改进。

直接上代码。

头文件:

#pragma once#include <QObject>
#include <QWidget>class MessageTips : public QWidget
{Q_OBJECTpublic:MessageTips(QString showStr = "", QWidget* parent = nullptr);double getOpacityValue() { return m_opacity; };void setOpacityValue(double value) { m_opacity = value; };int getTextSize() { return m_textSize; };void setTextSize(int value) { m_textSize = value; };QColor getTextColor() { return m_textColor; };void setTextColor(const QColor& value) { m_textColor = value; };QColor getBackgroundColor() { return m_bgColor; };void setBackgroundColor(const QColor& value) { m_bgColor = value; };QColor getFrameColor() { return m_frameColor; };void setFrameColor(const QColor& value) { m_frameColor = value; };int getFrameSize() { return m_frameSize; };void setFrameSize(int value) { m_frameSize = value; };int getShowTime() { return m_showTime; };void setShowTime(int value) { m_showTime = value; };void setCloseTimeSpeed(int closeTime = 100, double closeSpeed = 0.1);void prepare();static void showMessageTips(QString showStr, QWidget* parent = nullptr);
protected:void paintEvent(QPaintEvent* event) override;private:QString m_showStr;double m_opacity = 0.8;int     m_textSize = 18;//显示字体大小QColor  m_textColor = QColor(255, 255, 255);//字体颜色QColor  m_bgColor = QColor(192, 192, 192);//窗体的背景色QColor  m_frameColor = QColor(211, 211, 211);//边框颜色int     m_frameSize = 2;//边框粗细大小int     m_showTime = 3500;//显示时间int     m_closeTime = 100;//关闭需要时间double  m_closeSpeed = 0.1;//窗体消失的平滑度,大小0~1
};

cpp文件:

#include "messagetips.h"
#include <QDesktopWidget>
#include <QPainter>
#include <QTimer>
#include <QApplication>
#include <QDebug>MessageTips::MessageTips(QString showStr, QWidget* parent): QWidget(parent),m_showStr(showStr)
{setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::Tool);this->setAttribute(Qt::WA_TranslucentBackground); // ****这里很重要****this->setAttribute(Qt::WA_TransparentForMouseEvents, true);// 禁止鼠标事件this->setAttribute(Qt::WA_DeleteOnClose);this->setFixedHeight(50);QFont font("Arial", m_textSize, QFont::Bold);QFontMetrics fontMetrics(font);int tw = fontMetrics.width(m_showStr);if (tw > 200)this->setFixedWidth(tw + 50);elsethis->setFixedWidth(200);
}void MessageTips::prepare()
{this->setWindowOpacity(m_opacity);connect(this,SIGNAL(finished()),this,SLOT(onFinished()));QTimer* mtimer = new QTimer(this);//隐藏的定时器mtimer->setTimerType(Qt::PreciseTimer);connect(mtimer, &QTimer::timeout, this, [=]() {m_opacity = m_opacity - m_closeSpeed;if (m_opacity <= 0) {mtimer->stop();this->close();return;}elsethis->setWindowOpacity(m_opacity);    });//support for multi-screens.int scrIndex = 0;QDesktopWidget* desktop = QApplication::desktop();QWidget* acWgt = QApplication::activeWindow();if (acWgt){QPoint checkPoint(acWgt->pos().x() + acWgt->width() / 2, acWgt->pos().y() + acWgt->height() / 2);for (int i = 0; i < desktop->screenCount(); i++){if (desktop->screenGeometry(i).contains(checkPoint)){scrIndex = i;break;}}}QRect scrRect = desktop->screenGeometry(scrIndex);//设置屏幕居中显示this->move(scrRect.left() + (scrRect.width() - this->width()) / 2, scrRect.top() + (scrRect.height() - this->height()) / 2);QTimer::singleShot(m_showTime, [=]() {mtimer->start(m_closeTime); });//执行延时自动关闭
}void MessageTips::paintEvent(QPaintEvent* event)
{QPainter painter(this);painter.setBrush(QBrush(m_bgColor));//窗体的背景色painter.setPen(QPen(m_frameColor, m_frameSize));//窗体边框的颜色和笔画大小QRectF rect(0, 0, this->width(), this->height());painter.drawRoundedRect(rect, 10, 10); // round rect//文字显示居中,设置字体,大小,颜色QFont font("Arial", m_textSize, QFont::Bold);painter.setFont(font);painter.setPen(QPen(m_textColor, m_frameSize));painter.drawText(rect,Qt::AlignHCenter | Qt::AlignVCenter,m_showStr);
}//设置关闭的时间和速度,speed大小限定0~1
void MessageTips::setCloseTimeSpeed(int closeTime, double closeSpeed)
{if (closeSpeed > 0 && closeSpeed <= 1) m_closeSpeed = closeSpeed;m_closeTime = closeTime;
}void MessageTips::showMessageTips(QString showStr, QWidget* parent)
{MessageTips* mMessageTips = new MessageTips(showStr, parent);mMessageTips->setShowTime(1500);mMessageTips->prepare();mMessageTips->show();
}

调用方法:

		MessageTips::showMessageTips("OK",this);

运行效果:

运行效果与参考链接类似。

相关文章:

  • 源码部署ELK
  • 验证软件需求
  • Go语言的中间件(middleware)是如何实现的?
  • 2024年云南特岗教师报名流程,超详细,明天就开始报名哦!
  • C++设计模式之策略模式、迭代器模式、适配器模式、工厂模式、超级工厂模式、享元模式、代理模式
  • SpringBoot自动化配置原理
  • Cortex-M3的SysTick 定时器
  • linux文件权限常用知识点,基于Linux(openEuler、CentOS8)
  • 基于Rsoft的Fullwave仿真模块进行双芯波导能量耦合与波分复用
  • Java 18 新特性详解
  • 后量子密码解决方案
  • 5.Redis之常用数据结构单线程模型
  • Java整合ELK实现日志收集 之 Elasticsearch、Logstash、Kibana
  • 为 AWS 子账户添加安全组修改权限
  • 深入探索SQL注入:盲注技术及其防御策略
  • 2017届校招提前批面试回顾
  • 3.7、@ResponseBody 和 @RestController
  • ES6 学习笔记(一)let,const和解构赋值
  • Java 内存分配及垃圾回收机制初探
  • JavaScript 基础知识 - 入门篇(一)
  • java取消线程实例
  • PHP 7 修改了什么呢 -- 2
  • Shell编程
  • spring-boot List转Page
  • SpringBoot几种定时任务的实现方式
  • Stream流与Lambda表达式(三) 静态工厂类Collectors
  • webpack4 一点通
  • 漂亮刷新控件-iOS
  • 400多位云计算专家和开发者,加入了同一个组织 ...
  • scrapy中间件源码分析及常用中间件大全
  • #Linux(make工具和makefile文件以及makefile语法)
  • #中国IT界的第一本漂流日记 传递IT正能量# 【分享得“IT漂友”勋章】
  • $LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
  • (02)Unity使用在线AI大模型(调用Python)
  • (1)Map集合 (2)异常机制 (3)File类 (4)I/O流
  • (12)Hive调优——count distinct去重优化
  • (12)Linux 常见的三种进程状态
  • (2024,RWKV-5/6,RNN,矩阵值注意力状态,数据依赖线性插值,LoRA,多语言分词器)Eagle 和 Finch
  • (23)Linux的软硬连接
  • (24)(24.1) FPV和仿真的机载OSD(三)
  • (ZT)薛涌:谈贫说富
  • (笔试题)分解质因式
  • (附源码)ssm教材管理系统 毕业设计 011229
  • (十八)devops持续集成开发——使用docker安装部署jenkins流水线服务
  • (一)Linux+Windows下安装ffmpeg
  • .net core 微服务_.NET Core 3.0中用 Code-First 方式创建 gRPC 服务与客户端
  • .Net 访问电子邮箱-LumiSoft.Net,好用
  • .NET 设计一套高性能的弱事件机制
  • .NET/C# 检测电脑上安装的 .NET Framework 的版本
  • .NET/C# 利用 Walterlv.WeakEvents 高性能地定义和使用弱事件
  • .NET程序集编辑器/调试器 dnSpy 使用介绍
  • .NET多线程执行函数
  • /etc/apt/sources.list 和 /etc/apt/sources.list.d
  • ;号自动换行
  • @Builder注释导致@RequestBody的前端json反序列化失败,HTTP400