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);
}
相关推荐
Hello.Reader1 小时前
Redis 延迟监控深度指南
数据库·redis·缓存
ybq195133454311 小时前
Redis-主从复制-分布式系统
java·数据库·redis
weixin_472339462 小时前
高效处理大体积Excel文件的Java技术方案解析
java·开发语言·excel
枯萎穿心攻击3 小时前
响应式编程入门教程第二节:构建 ObservableProperty<T> — 封装 ReactiveProperty 的高级用法
开发语言·unity·c#·游戏引擎
Eiceblue4 小时前
【免费.NET方案】CSV到PDF与DataTable的快速转换
开发语言·pdf·c#·.net
好奇的菜鸟4 小时前
如何在IntelliJ IDEA中设置数据库连接全局共享
java·数据库·intellij-idea
tan180°4 小时前
MySQL表的操作(3)
linux·数据库·c++·vscode·后端·mysql
m0_555762905 小时前
Matlab 频谱分析 (Spectral Analysis)
开发语言·matlab
满昕欢喜5 小时前
SQL Server从入门到项目实践(超值版)读书笔记 20
数据库·sql·sqlserver
浪裡遊5 小时前
React Hooks全面解析:从基础到高级的实用指南
开发语言·前端·javascript·react.js·node.js·ecmascript·php