标签:
简单的做了一个类似QQ消息提示的消息弹窗提示的小模块,便于在系统后台程序提示检测的信息,使用Qt开发,设计整体思路是做一个无框架的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 }
标签:
原文地址:http://www.cnblogs.com/nickfox/p/5569602.html