Qt 实现桌面右下角消息弹窗提示

来源:互联网 发布:mac版unturned怎么汉化 编辑:程序博客网 时间:2024/06/05 07:34

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

 

一、弹窗主体部分 class widget

#include "widget.h"#include "titlewidget.h"#include <QVBoxLayout>#include "mypushbutton.h"#include <QLabel>#include <QPainter>#include <QBitmap>#include <QDesktopWidget>#include <QApplication>#include <QTimer>#include <QDesktopServices> Widget::Widget(QWidget *parent) :    QWidget(parent){    setWindowFlags(Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);isEnter = false;    titleW=new titleWidget;    connect(titleW,SIGNAL(myClose()),this,SLOT(close()));     content=new QLabel;    content->setWordWrap(true);    content->setAlignment(Qt::AlignTop);    content->setFixedSize(300,100);    btnLook=new myPushButton("look.png","查看");    connect(btnLook,SIGNAL(clicked()),this,SLOT(seeInfo()));     QVBoxLayout*mainLayout=new QVBoxLayout;    mainLayout->setMargin(0);    mainLayout->addWidget(titleW);    mainLayout->addWidget(content);    content->setMargin(5);    mainLayout->addWidget(btnLook,0,Qt::AlignRight);    setLayout(mainLayout);     setFixedSize(sizeHint().width(),sizeHint().height());     timerShow=new QTimer(this);    connect(timerShow,SIGNAL(timeout()),this,SLOT(myMove()));    timerStay=new QTimer(this);    connect(timerStay,SIGNAL(timeout()),this,SLOT(myStay()));    timerClose=new QTimer(this);    connect(timerClose,SIGNAL(timeout()),this,SLOT(myClose()));} Widget::~Widget(){} void Widget::setMsg(QString title, QString content, QString work){    titleW->setTitleText("  "+title);    this->content->setText("   "+content);} void Widget::paintEvent(QPaintEvent *){    QBitmap bitmap(this->size());    bitmap.fill(Qt::white);    QPainter painter(this);    painter.setBrush(QBrush(QColor(250,240,230)));    painter.setPen(QPen(QBrush(QColor(255,222,173)),4));    painter.drawRoundedRect(bitmap.rect(),5,5);    setMask(bitmap);}void Widget::showAsQQ(){    QDesktopWidget *deskTop=QApplication::desktop();    deskRect=deskTop->availableGeometry();     normalPoint.setX(deskRect.width()-rect().width()-1);    normalPoint.setY(deskRect.height()-rect().height());    move(normalPoint.x(),deskRect.height()-1);    show();    timerShow->start(5);}//平滑显示出来void Widget::myMove(){    static int beginY=QApplication::desktop()->height();    beginY--;    move(normalPoint.x(),beginY);    if(beginY<=normalPoint.y())    {        timerShow->stop();        timerStay->start(1000);    }}//停留显示void Widget::myStay(){    static int timeCount=0;    timeCount++;    if(timeCount>=9)    {        timerStay->stop();        timerClose->start(200);    }}//自动关闭时实现淡出效果void Widget::myClose(){static double tran=1.0;if(isEnter){tran = 1.0;setWindowOpacity(tran);return;}    tran-=0.1;    if(tran<=0.0)    {        timerClose->stop();        emit close();    }    else        setWindowOpacity(tran);}void Widget::seeInfo(){    } void Widget::enterEvent(QEvent *){isEnter = true;}void Widget::leaveEvent(QEvent *){    isEnter = false;}

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

//标题类 class titlewidget#include "titlewidget.h"#include <QLabel>#include <QToolButton>#include <QHBoxLayout>#include <QPainter>#include <QLinearGradient>#include <QIcon>titleWidget::titleWidget(QWidget *parent) :    QWidget(parent){    titleText=new QLabel;    btnClose = new QToolButton(this);btnClose->setObjectName(QString::fromUtf8("btnClose"));btnClose->setToolTip(tr("关闭"));btnClose->setStyleSheet(QString::fromUtf8("QWidget[objectName=\"btnClose\"]{\n""border-width: 0px;\n""border-style: solid;\n""padding: 0px;\n""padding-left: 0px;\n""padding-right: 0px;\n""min-width: 16px;\n""max-width: 16px;\n""min-height: 16px;\n""max-height: 16px;\n""background-image: url(:/Resources/btn_close_normal.bmp);\n""}\n""\n""QWidget[objectName=\"btnClose\"]:hover{\n""background-image: url(:/Resources/btn_close_highlight.bmp);\n""}\n""\n""QWidget[objectName=\"btnClose\"]:pressed{\n""background-image: url(:/Resources/btn_close_down.bmp);\n""}\n"""));    connect(btnClose,SIGNAL(clicked()),this,SIGNAL(myClose()));    QHBoxLayout *layout=new QHBoxLayout;    layout->addWidget(titleText,0,Qt::AlignLeft);    layout->addStretch();    layout->addWidget(btnClose,0,Qt::AlignRight);    layout->setMargin(0);    setLayout(layout);    setFixedHeight(20);}void titleWidget::paintEvent(QPaintEvent *){    QLinearGradient linear(rect().topLeft(),rect().bottomRight());    linear.setColorAt(0,QColor(227,207,87));    linear.setColorAt(0.5,QColor(245,222,179));    linear.setColorAt(1,QColor(189,252,201));    QPainter painter(this);    painter.setBrush(QBrush(linear));    painter.setPen(Qt::NoPen);    painter.drawRect(rect());}void titleWidget::setTitleText(QString title){    titleText->setText(title);}//按钮类:class mypushbutton#include "mypushbutton.h"#include <QPalette>#include <QPixmap>#include <QCursor>myPushButton::myPushButton(QWidget *parent) :    QPushButton(parent){}myPushButton::myPushButton(QString iconStr,QString textStr, QWidget *parent):QPushButton(parent){    QPixmap pixmap(":/Resources/"+iconStr);    setIcon(QIcon(pixmap));    setIconSize(pixmap.size());setText(textStr);    resize(pixmap.size());    setBkPalette(0);//设置背景完全透明    setFlat(true);    setAutoFillBackground(true);}void myPushButton::setBkPalette(int transparency)//设置背景透明度{   QPalette palette;   palette.setBrush(QPalette::Button,QBrush(QColor(255,255,255,transparency)));   setPalette(palette);}void myPushButton::enterEvent(QEvent *){setCursor(Qt::PointingHandCursor);}void myPushButton::leaveEvent(QEvent *){    setCursor(Qt::CustomCursor);}void myPushButton::mousePressEvent(QMouseEvent *e){    }void myPushButton::mouseReleaseEvent(QMouseEvent *e){emit clicked();}
原创粉丝点击