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);
}
相关推荐
forestsea29 分钟前
MySQL 调优
数据库·mysql·性能优化
Java手札1 小时前
Windows下Golang与Nuxt项目宝塔部署指南
开发语言·windows·golang
小生凡一1 小时前
腾讯二面:TCC分布式事务 | 图解TCC|用Go语言实现一个TCC
开发语言·分布式·golang
minji...1 小时前
C语言 函数递归
c语言·开发语言·算法
云上空1 小时前
C#初级知识总结
开发语言·c#
松树戈1 小时前
PostgreSQL使用LIKE右模糊没有走索引分析&验证
数据库·postgresql
文牧之2 小时前
PostgreSQL 常用日志
运维·数据库·postgresql
钢铁男儿2 小时前
C# 深入理解类:面向对象编程的核心数据结构
开发语言·数据结构·c#
karatttt2 小时前
用go从零构建写一个RPC(仿gRPC,tRPC)--- 版本1
后端·qt·rpc·架构·golang
TE-茶叶蛋2 小时前
Redis 原子操作
数据库·redis·缓存