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);
}
相关推荐
iCxhust10 分钟前
c# U盘映像生成工具
开发语言·单片机·c#
yangzhi_emo1 小时前
ES6笔记2
开发语言·前端·javascript
互联网搬砖老肖1 小时前
运维打铁: MongoDB 数据库集群搭建与管理
运维·数据库·mongodb
emplace_back2 小时前
C# 集合表达式和展开运算符 (..) 详解
开发语言·windows·c#
jz_ddk2 小时前
[学习] C语言数学库函数背后的故事:`double erf(double x)`
c语言·开发语言·学习
萧曵 丶2 小时前
Rust 所有权系统:深入浅出指南
开发语言·后端·rust
xiaolang_8616_wjl2 小时前
c++文字游戏_闯关打怪2.0(开源)
开发语言·c++·开源
典学长编程2 小时前
数据库Oracle从入门到精通!第四天(并发、锁、视图)
数据库·oracle
收破烂的小熊猫~2 小时前
《Java修仙传:从凡胎到码帝》第四章:设计模式破万法
java·开发语言·设计模式