当前位置: 首页 > news >正文

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

一、效果 ![](https://i-blog.csdnimg.cn/direct/f02739adb0de455583c97cc889a83fb1.png)

二、弹窗主体部分 noticewidget

/*
**  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);
}

相关文章:

  • 【滑动窗口+哈希表/数组记录】Leetcode 76. 最小覆盖子串
  • 解决ssh拉取服务器数据,要多次输入密码的问题
  • 基于ssm的同城上门维修平台管理系统(源码+数据库)
  • 矫平机深度解析:操作实务、行业标准与智能化升级
  • 【金仓数据库征文】交通行业的国产化数据库替换之金仓数据库KingbaseES应用实践
  • FlinkJobmanager深度解析
  • 【中级软件设计师】函数调用 —— 传值调用和传地址调用 (附软考真题)
  • C#中实现XML解析器
  • Vue 3 父子组件通信案例详解:Props 与 Emits 实战
  • MySQL 详解之用户、权限与审计:保障数据安全的基石
  • MCP协议:AI与工具无缝连接的“万能插头“及最佳实践指南
  • MySQL 详解之函数:数据处理与计算的利器
  • SQLMesh 表格对比指南:深入理解 table_diff 工具的实际应用
  • 每日两道leetcode(补充一)
  • 【FreeRTOS】事件标志组
  • 58、微服务保姆教程(一)
  • 第十一天 主菜单/设置界面 过场动画(Timeline) 成就系统(Steam/本地) 多语言支持
  • HTML 模板技术与服务端渲染
  • 2022李宏毅老师机器学习课程笔记
  • 【尚硅谷Redis6】自用学习笔记
  • 日均新开三家“首店”,上海的“首发经济”密码是什么?
  • 双拥主题歌曲MV:爱我人民,爱我军
  • 期待会师!神二十与空间站完成对接
  • 秦洪看盘|避险情绪升温,短线或延续相对钝化状态
  • 【社论】上海经济开门红:不偏科、挑大梁
  • 神二十明日发射,长二F火箭推进剂加注工作已完成