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

一、效果 ![

二、弹窗主体部分 noticewidget

cpp 复制代码
/*
**  File name:   NoticeWidget.h
**  Author:      
**  Date:        2025-04-25
**  Brief:       通知栏控件
**  Copyright (C) [email protected] 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);
}
相关推荐
星垣矩阵架构师7 分钟前
架构设计之存储高性能——非关系型数据库(NoSQL)
数据库·架构·nosql
明月看潮生16 分钟前
青少年编程与数学 01-011 系统软件简介 16 Redis数据库
数据库·redis·青少年编程·编程与数学
明月看潮生19 分钟前
青少年编程与数学 01-011 系统软件简介 15 MongoDB数据库
数据库·mongodb·青少年编程·编程与数学
宋一平工作室28 分钟前
单片机队列功能模块的实战和应用
c语言·开发语言·stm32·单片机·嵌入式硬件
笨笨马甲38 分钟前
Qt Http Server模块功能及架构
qt·http·架构
豆豆(设计前端)41 分钟前
在 JavaScript 中,你可以使用 Date 对象来获取 当前日期 和 当前时间、当前年份。
开发语言·javascript·ecmascript
喵叔哟41 分钟前
第7章:Neo4j索引与约束
数据库·oracle·neo4j
freyazzr1 小时前
TCP/IP 网络编程 | Reactor事件处理模式
开发语言·网络·c++·网络协议·tcp/ip
电院工程师1 小时前
SM3算法Python实现(无第三方库)
开发语言·python·算法·安全·密码学
Winn~1 小时前
MySQL行锁、记录锁、间隙锁、临建锁、意向锁、表锁
数据库·mysql