简单的做了一个类似QQ消息提示的消息弹窗提示的小模块,便于在系统后台程序提示检测的信息,使用Qt开发,设计整体思路是做一个无框架的widget,自己实现标题栏和内容栏,添加了向上移出,自动消隐退出效果,窗体简单,模块结构便于以后进行扩展和移植,旨在显示文字信息,通过按钮操作与主程序进行通信,运行结果如图

一、弹窗主体部分 class widget

  1 #include "widget.h"
  2
  3 #include "titlewidget.h"
  4
  5 #include <QVBoxLayout>
  6
  7 #include "mypushbutton.h"
  8
  9 #include <QLabel>
 10
 11 #include <QPainter>
 12
 13 #include <QBitmap>
 14
 15 #include <QDesktopWidget>
 16
 17 #include <QApplication>
 18
 19 #include <QTimer>
 20
 21 #include <QDesktopServices>
 22
 23
 24
 25 Widget::Widget(QWidget *parent) :
 26
 27     QWidget(parent)
 28
 29 {
 30
 31     setWindowFlags(Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);
 32
 33 isEnter = false;
 34
 35     titleW=new titleWidget;
 36
 37     connect(titleW,SIGNAL(myClose()),this,SLOT(close()));
 38
 39
 40
 41     content=new QLabel;
 42
 43     content->setWordWrap(true);
 44
 45     content->setAlignment(Qt::AlignTop);
 46
 47     content->setFixedSize(300,100);
 48
 49     btnLook=new myPushButton("look.png","查看");
 50
 51     connect(btnLook,SIGNAL(clicked()),this,SLOT(seeInfo()));
 52
 53
 54
 55     QVBoxLayout*mainLayout=new QVBoxLayout;
 56
 57     mainLayout->setMargin(0);
 58
 59     mainLayout->addWidget(titleW);
 60
 61     mainLayout->addWidget(content);
 62
 63     content->setMargin(5);
 64
 65     mainLayout->addWidget(btnLook,0,Qt::AlignRight);
 66
 67     setLayout(mainLayout);
 68
 69
 70
 71     setFixedSize(sizeHint().width(),sizeHint().height());
 72
 73
 74
 75     timerShow=new QTimer(this);
 76
 77     connect(timerShow,SIGNAL(timeout()),this,SLOT(myMove()));
 78
 79     timerStay=new QTimer(this);
 80
 81     connect(timerStay,SIGNAL(timeout()),this,SLOT(myStay()));
 82
 83     timerClose=new QTimer(this);
 84
 85     connect(timerClose,SIGNAL(timeout()),this,SLOT(myClose()));
 86
 87 }
 88
 89
 90
 91 Widget::~Widget()
 92
 93 {
 94
 95 }
 96
 97
 98
 99 void Widget::setMsg(QString title, QString content, QString work)
100
101 {
102
103     titleW->setTitleText("  "+title);
104
105     this->content->setText("   "+content);
106
107 }
108
109
110
111 void Widget::paintEvent(QPaintEvent *)
112
113 {
114
115     QBitmap bitmap(this->size());
116
117     bitmap.fill(Qt::white);
118
119     QPainter painter(this);
120
121     painter.setBrush(QBrush(QColor(250,240,230)));
122
123     painter.setPen(QPen(QBrush(QColor(255,222,173)),4));
124
125     painter.drawRoundedRect(bitmap.rect(),5,5);
126
127     setMask(bitmap);
128
129 }
130
131 void Widget::showAsQQ()
132
133 {
134
135     QDesktopWidget *deskTop=QApplication::desktop();
136
137     deskRect=deskTop->availableGeometry();
138
139
140
141     normalPoint.setX(deskRect.width()-rect().width()-1);
142
143     normalPoint.setY(deskRect.height()-rect().height());
144
145     move(normalPoint.x(),deskRect.height()-1);
146
147     show();
148
149     timerShow->start(5);
150
151 }
152
153 //平滑显示出来
154
155 void Widget::myMove()
156
157 {
158
159     static int beginY=QApplication::desktop()->height();
160
161     beginY--;
162
163     move(normalPoint.x(),beginY);
164
165     if(beginY<=normalPoint.y())
166
167     {
168
169         timerShow->stop();
170
171         timerStay->start(1000);
172
173     }
174
175 }
176
177 //停留显示
178
179 void Widget::myStay()
180
181 {
182
183     static int timeCount=0;
184
185     timeCount++;
186
187     if(timeCount>=9)
188
189     {
190
191         timerStay->stop();
192
193         timerClose->start(200);
194
195     }
196
197 }
198
199 //自动关闭时实现淡出效果
200
201 void Widget::myClose()
202
203 {
204
205 static double tran=1.0;
206
207 if(isEnter){
208
209 tran = 1.0;
210
211 setWindowOpacity(tran);
212
213 return;
214
215 }
216
217     tran-=0.1;
218
219     if(tran<=0.0)
220
221     {
222
223         timerClose->stop();
224
225         emit close();
226
227     }
228
229     else
230
231         setWindowOpacity(tran);
232
233 }
234
235 void Widget::seeInfo()
236
237 {
238
239
240
241 }
242
243
244
245 void Widget::enterEvent(QEvent *)
246
247 {
248
249 isEnter = true;
250
251 }
252
253 void Widget::leaveEvent(QEvent *)
254
255 {
256
257     isEnter = false;
258
259 }

二、标题部分与自定义按钮部分

  1 //标题类 class titlewidget
  2 #include "titlewidget.h"
  3 #include <QLabel>
  4 #include <QToolButton>
  5 #include <QHBoxLayout>
  6 #include <QPainter>
  7 #include <QLinearGradient>
  8 #include <QIcon>
  9
 10 titleWidget::titleWidget(QWidget *parent) :
 11     QWidget(parent)
 12 {
 13     titleText=new QLabel;
 14     btnClose = new QToolButton(this);
 15     btnClose->setObjectName(QString::fromUtf8("btnClose"));
 16     btnClose->setToolTip(tr("关闭"));
 17     btnClose->setStyleSheet(QString::fromUtf8("QWidget[objectName=\"btnClose\"]{\n"
 18         "border-width: 0px;\n"
 19         "border-style: solid;\n"
 20         "padding: 0px;\n"
 21         "padding-left: 0px;\n"
 22         "padding-right: 0px;\n"
 23         "min-width: 16px;\n"
 24         "max-width: 16px;\n"
 25         "min-height: 16px;\n"
 26         "max-height: 16px;\n"
 27         "background-image: url(:/Resources/btn_close_normal.bmp);\n"
 28         "}\n"
 29         "\n"
 30         "QWidget[objectName=\"btnClose\"]:hover{\n"
 31         "background-image: url(:/Resources/btn_close_highlight.bmp);\n"
 32         "}\n"
 33         "\n"
 34         "QWidget[objectName=\"btnClose\"]:pressed{\n"
 35         "background-image: url(:/Resources/btn_close_down.bmp);\n"
 36         "}\n"
 37         ""));
 38     connect(btnClose,SIGNAL(clicked()),this,SIGNAL(myClose()));
 39     QHBoxLayout *layout=new QHBoxLayout;
 40     layout->addWidget(titleText,0,Qt::AlignLeft);
 41     layout->addStretch();
 42     layout->addWidget(btnClose,0,Qt::AlignRight);
 43     layout->setMargin(0);
 44     setLayout(layout);
 45     setFixedHeight(20);
 46 }
 47
 48 void titleWidget::paintEvent(QPaintEvent *)
 49 {
 50     QLinearGradient linear(rect().topLeft(),rect().bottomRight());
 51     linear.setColorAt(0,QColor(227,207,87));
 52     linear.setColorAt(0.5,QColor(245,222,179));
 53     linear.setColorAt(1,QColor(189,252,201));
 54
 55     QPainter painter(this);
 56     painter.setBrush(QBrush(linear));
 57     painter.setPen(Qt::NoPen);
 58     painter.drawRect(rect());
 59 }
 60
 61 void titleWidget::setTitleText(QString title)
 62 {
 63     titleText->setText(title);
 64 }
 65 //按钮类:class mypushbutton
 66 #include "mypushbutton.h"
 67 #include <QPalette>
 68 #include <QPixmap>
 69 #include <QCursor>
 70
 71 myPushButton::myPushButton(QWidget *parent) :
 72     QPushButton(parent)
 73 {
 74 }
 75 myPushButton::myPushButton(QString iconStr,QString textStr, QWidget *parent):QPushButton(parent)
 76 {
 77     QPixmap pixmap(":/Resources/"+iconStr);
 78     setIcon(QIcon(pixmap));
 79     setIconSize(pixmap.size());
 80     setText(textStr);
 81     resize(pixmap.size());
 82     setBkPalette(0);//设置背景完全透明
 83     setFlat(true);
 84     setAutoFillBackground(true);
 85 }
 86
 87 void myPushButton::setBkPalette(int transparency)//设置背景透明度
 88 {
 89    QPalette palette;
 90    palette.setBrush(QPalette::Button,QBrush(QColor(255,255,255,transparency)));
 91    setPalette(palette);
 92 }
 93 void myPushButton::enterEvent(QEvent *)
 94 {
 95     setCursor(Qt::PointingHandCursor);
 96 }
 97 void myPushButton::leaveEvent(QEvent *)
 98 {
 99     setCursor(Qt::CustomCursor);
100 }
101 void myPushButton::mousePressEvent(QMouseEvent *e)
102 {
103
104 }
105 void myPushButton::mouseReleaseEvent(QMouseEvent *e)
106 {
107
108     emit clicked();
109 }

转载于:https://www.cnblogs.com/nickfox/p/5569602.html

Qt 实现桌面右下角消息弹窗提示相关推荐

  1. html5 模仿qq下拉,JS封装的模仿qq右下角消息弹窗功能示例

    本文实例讲述了JS封装的模仿qq右下角消息弹窗功能.分享给大家供大家参考,具体如下: 在我们的日常开发中,或者生活中,经常需要用到弹出窗.这里我们就用js模拟一下qq消息一样的弹出窗. 直接贴代码: ...

  2. Win11怎么关桌面右下角视频播放的提示?

    参考:Win11怎么关桌面右下角视频播放的提示? 1 我们在浏览器中播放视频时会在桌面右下角显示媒体控件 2 我们打开Microsoft Edge浏览器,点击右上角的三点,然后点击设置 chrome浏 ...

  3. 使用java在windows桌面右下角弹出提示框

    使用java在windows桌面右下角弹出提示框 最终效果图 核心思想 java代码 测试运行 最终效果图 核心思想 使用java.swing.JDialog创建一个窗体对象,设置窗体的展示坐标值以及 ...

  4. C# 模仿QQ右下角 消息闪烁提示

    主要是使用自带的notifyIcon这个控件即可,用两个icon图片循环切换,效果即闪烁提示. 主要代码如下: public partial class Form1 : Form{int i = 0; ...

  5. Qt实现桌面右下角放置窗体

    代码如下: Q_Widget * dlgRight = new Q_Widget(NULL);QDesktopWidget *deskTop=QApplication::desktop();auto ...

  6. C#语言实例源码系列-实现桌面右下角Pop弹窗

    专栏分享 点击跳转=>Unity3D特效百例 点击跳转=>案例项目实战源码 点击跳转=>游戏脚本-辅助自动化 点击跳转=>Android控件全解手册

  7. 获取大麦网孟鹤堂演出数据并播报和在右下角弹窗提示

    #!/usr/bin/env python # coding=utf-8#!/usr/bin/env python # coding=utf-8 # 获取大麦网孟鹤堂演出数据并播报和在右下角弹窗提示i ...

  8. 结合C++,网页实现消息即时提醒(桌面右下角弹窗)

    吃饭的时候 ,突然有所感悟.欣喜若狂. 首先这个不是C++外框包含网页 , 避免webbrower与一些页面的操作的不兼容性. 就是不知道这么想的,我是不是第一个.不然也能称为是一个创新. 这个想法的 ...

  9. QML实现桌面右下角弹窗

    实现效果 这次制作的桌面右下角弹窗,主要功能有透明度和位置动画.定时关闭.鼠标放在上面暂留. 实现思路 首先,我们需要获取桌面大小,然后 move 到右下角去,这里借助的 Screen: //初始位置 ...

最新文章

  1. oracle查看临时表空间文件,Oracle-临时表空间
  2. 探秘Tomcat——一个简易的Servlet容器
  3. Servlet 实例
  4. java 连接池技术_java数据库连接池技术原理(浅析)
  5. Java练习 SDUT-1586_计算组合数
  6. activereports_报表 ActiveReports 迎来 .Net Core 时代!一键创建 .Net Core 项目
  7. leetcode 456. 132 Pattern 132模式 题解(栈)
  8. 简化document.createElement(div)动态生成层方法
  9. tf.keras与 TensorFlow混用,trainable=False设置无效
  10. 【鱼眼镜头7】如何利用鱼眼镜头测距
  11. php 表单提交渲染问题,如何解决php表单提交的数据丢失的问题
  12. c语言递归算法简单例子加讲解,递归算法经典例子C语言实现递归算法
  13. mac如何配置环境变量
  14. ubuntu18.04安装evo测评工具踩坑
  15. 习题11-7 奇数值结点链表 (20分)
  16. C语言uint8_t和char的区别,c – int8_t和uint8_t是char类型吗?
  17. 使用Git向Github上传项目(包含大文件>25mb)
  18. Response总结
  19. 变量被重复定义的问题
  20. day 32 css后续补充以及js 简单入门

热门文章

  1. MRT(MODIS Reprojection Tool) 提取数据
  2. PADS VX2.x安装教程
  3. Photoshop2018详细安装教程
  4. 串行异步通信_单片机串行口介绍
  5. html5实例异步图片加载,javascript – 你如何处理html5的画布图像加载异步?
  6. java自定义jsp标签_深入浅出javaWeb实战第17讲自定义JSP标签(上)
  7. MFC之ComboBox控件用法
  8. java实战调用数据库_实战php调用java类由java类读数据库完成相关操作(InberWrite)_PHP...
  9. jmeter服务器测试项目,JMeter-项目测试
  10. 八年级下册册计算机计划,粤教版(B版)信息技术八年级下册教学工作计划