Qt实现桌面右下角消息弹窗提示
Qt实现桌⾯右下⾓消息弹窗提⽰
  简单的做了⼀个类似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
qq消息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());
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
99void Widget::setMsg(QString title, QString content, QString work) 100
101 {
102
103    titleW->setTitleText(""+title);
104
105this->content->setText(""+content);
106
107 }
108
109
110
111void 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.(),5,5);
126
127    setMask(bitmap);
128
129 }
130
131void 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
155void Widget::myMove()
159static int beginY=QApplication::desktop()->height(); 160
161    beginY--;
162
163    move(normalPoint.x(),beginY);
164
165if(beginY<=normalPoint.y())
166
167    {
168
169        timerShow->stop();
170
171        timerStay->start(1000);
172
173    }
174
175 }
176
177//停留显⽰
178
179void Widget::myStay()
180
181 {
182
183static int timeCount=0;
184
185    timeCount++;
186
187if(timeCount>=9)
188
189    {
190
191        timerStay->stop();
192
193        timerClose->start(200);
国内十大男装品牌
194
195    }
196
197 }
198
199//⾃动关闭时实现淡出效果
200
201void Widget::myClose()
202
203 {
204
205static double tran=1.0;
206
207if(isEnter){
208
209 tran = 1.0;
210
赞美老师的诗句211 setWindowOpacity(tran);
212
213return;
软件测试原理214
215 }
216
217    tran-=0.1;
218
219if(tran<=0.0)
220
221    {
222
223        timerClose->stop();
224
225        emit close();
226
227    }
228
229else
230
231        setWindowOpacity(tran);
232
233 }
234
235void Widget::seeInfo()
236
237 {
238
239
243
244
245void Widget::enterEvent(QEvent *)
246
247 {
248
249 isEnter = true;
250
251 }
252
253void 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
48void 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
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
87void myPushButton::setBkPalette(int transparency)//设置背景透明度
88 {
89    QPalette palette;
90    palette.setBrush(QPalette::Button,QBrush(QColor(255,255,255,transparency)));
91    setPalette(palette);
92 }
93void myPushButton::enterEvent(QEvent *)
94 {
95    setCursor(Qt::PointingHandCursor);
96 }
97void myPushButton::leaveEvent(QEvent *)
98 {
99    setCursor(Qt::CustomCursor);
100 }
101void myPushButton::mousePressEvent(QMouseEvent *e)
102 {
103
104 }
105void myPushButton::mouseReleaseEvent(QMouseEvent *e)
106 {
107
108    emit clicked();
109 }

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。