QT开发技术【QT实现桌面右下角消息】

一、效果 ![

二、弹窗主体部分 noticewidget

cpp 复制代码
/*
**  File name:   NoticeWidget.h
**  Author:      
**  Date:        2025-04-25
**  Brief:       通知栏控件
**  Copyright (C) 1392019713@qq.com All rights reserved.
*/

#include "../Include/NoticeWidget.h"
#include <QVBoxLayout>
#include <QPainter>
#include <QBitmap>
#include <QDesktopWidget>
#include <QDesktopServices>
#include <QApplication>

CNoticeWidget::CNoticeWidget(QWidget* parent)
	: QWidget(parent)
{
	m_bMouseEnter = false;
	setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
	setAttribute(Qt::WA_TranslucentBackground);

	m_pTitleLabel = new QLabel(this);
	m_pTitleLabel->setAlignment(Qt::AlignTop | Qt::AlignLeft);
	m_pTitleLabel->setFixedSize(300, 20);
	m_pTitleLabel->setMargin(5);

	m_pMessageLabel = new QLabel(this);
	m_pMessageLabel->setAlignment(Qt::AlignTop);
	m_pMessageLabel->setWordWrap(true);
	m_pMessageLabel->setFixedSize(300, 100);

	QHBoxLayout* m_pTitleLayout = new QHBoxLayout;
	m_pTitleLayout->setMargin(0);
	m_pTitleLayout->addWidget(m_pTitleLabel);
	m_pbtnlose = new QPushButton(this);
	m_pbtnlose->setStyleSheet("QPushButton { color: white; border: none; }");
	connect(m_pbtnlose, &QPushButton::clicked, this, &CNoticeWidget::close);
	m_pbtnlose->setFixedSize(20, 20);
	m_pbtnlose->setIcon(QIcon(":/Delete.png"));
	m_pbtnlose->setIconSize(QSize(12, 12));
	m_pTitleLayout->addWidget(m_pbtnlose);

	QVBoxLayout* m_pLayout = new QVBoxLayout;
	m_pLayout->setMargin(0);
	m_pLayout->addLayout(m_pTitleLayout);
	m_pLayout->addWidget(m_pMessageLabel);
	m_pMessageLabel->setMargin(5);
	setLayout(m_pLayout);
	setFixedSize(sizeHint().width(), sizeHint().height());

	m_pShowTimer = new QTimer(this);
	connect(m_pShowTimer, &QTimer::timeout, this, [this]()
		{
			static int nBeginY = QApplication::desktop()->height();
			nBeginY--;
			move(m_ptShowPos.x(), nBeginY);
			if (nBeginY <= m_ptShowPos.y())
			{
				m_pShowTimer->stop();
				m_pStayTimer->start(1000);
			}
		});

	m_pStayTimer = new QTimer(this);
	connect(m_pStayTimer, &QTimer::timeout, this, [this]()
		{
			static int nTimeCount = 0;
			nTimeCount++;
			if (nTimeCount >= 9)
			{
				m_pStayTimer->stop();
				m_pCloseTimer->start(200);
			}
		});

	m_pCloseTimer = new QTimer(this);
	connect(m_pCloseTimer, &QTimer::timeout, this, [this]()
		{
			static int nTran = 1.0;
			if (m_bMouseEnter)
			{
				nTran = 1.0;
				setWindowOpacity(nTran);
				return;
			}

			nTran -= 0.1;

			if (nTran <= 0.0)
			{
				m_pCloseTimer->stop();
				close();
			}
			else
			{
				setWindowOpacity(nTran);
			}
		});
}

CNoticeWidget::~CNoticeWidget()
{

}

void CNoticeWidget::SetMessage(const QString& qstrTitle, const QString& qstrMessage)
{

	m_pTitleLabel->setText(qstrTitle);

	m_pMessageLabel->setText( "  " + qstrMessage);
	
	QDesktopWidget* pDeskTop = QApplication::desktop();

	QRect rcDeskTop = pDeskTop->availableGeometry();

	m_ptShowPos = QPoint(rcDeskTop.width() - width() - 1, rcDeskTop.height() - height());

	move(m_ptShowPos.x(), rcDeskTop.height() - 1);

	show();

	m_pShowTimer->start(5);
}

void CNoticeWidget::paintEvent(QPaintEvent* event)
{
	QBitmap bitmap(size());
	bitmap.fill(Qt::white);
	QPainter painter(this);
	painter.setBrush(QBrush(QColor(250, 240, 230)));
	painter.setPen(QPen(QBrush(QColor(250, 222, 173)), 4));

	painter.drawRoundedRect(bitmap.rect(), 5, 5);

	setMask(bitmap);
	QWidget::paintEvent(event);
}

void CNoticeWidget::enterEvent(QEvent* event)
{
	m_bMouseEnter = true;
	QWidget::enterEvent(event);
}

void CNoticeWidget::leaveEvent(QEvent* event)
{
	m_bMouseEnter = false;
	QWidget::leaveEvent(event);
}
相关推荐
诗书画唱2 分钟前
【前端面试题】JavaScript 核心知识点解析(第二十二题到第六十一题)
开发语言·前端·javascript
冬天vs不冷2 分钟前
Java基础(九):Object核心类深度剖析
java·开发语言·python
TS的美梦3 分钟前
【1:1复刻R版】python版火山图函数一键出图
开发语言·python·r语言·scanpy·火山图
眠りたいです37 分钟前
Qt音频播放器项目实践:文件过滤、元数据提取与动态歌词显示实现
c++·qt·ui·音视频·媒体·qt5·mime
陈天伟教授1 小时前
(二)Python + 地球信息科学与技术 (GeoICT)=?
开发语言·python
异世界贤狼转生码农1 小时前
MongoDB Windows 系统实战手册:从配置到数据处理入门
数据库·mongodb
QuZhengRong1 小时前
【数据库】Navicat 导入 Excel 数据乱码问题的解决方法
android·数据库·excel
码农阿豪1 小时前
Windows从零到一安装KingbaseES数据库及使用ksql工具连接全指南
数据库·windows
时序数据说7 小时前
时序数据库市场前景分析
大数据·数据库·物联网·开源·时序数据库
七七&55610 小时前
2024年08月13日 Go生态洞察:Go 1.23 发布与全面深度解读
开发语言·网络·golang