参照原文:qt 消息弹出框 ,无框,缓慢自动消失
在根据原文作者的基础上对代码进行了修改,修复了提示框水平方向不居中,使用时受父类影响致颜色修改失效,定时器未销毁等bug,以及按照自己的编码习惯进行了重新编码
适用版本:Qt5
效果演示:
主窗体调用:
void Widget::on_pushButton_clicked()
{
MessageTips *mTips=new MessageTips("我藏在人群中然后失去晴空");
mTips->show();
}
这里和原文调用方式不同,不再继承this(否则会受到父类的样式表影响导致修改颜色等操作失败),而是在MessageTips的构造函数中设置关闭窗口自动调用析构函数
messagetips.h
#ifndef MESSAGETIPS_H
#define MESSAGETIPS_H
#include <QWidget>
class QHBoxLayout;
class QLabel;
class MessageTips : public QWidget
{
Q_OBJECT
public:
explicit MessageTips(QString showStr="默认显示", QWidget *parent = nullptr);
~MessageTips();
//设置背景颜色
void setBackgroundColor(const QColor &value);
//设置文本颜色
void setTextColor(const QColor &value);
//设置文本字体大小
void setTextSize(int value);
//设置显示时间
void setShowTime(int value);
//设置关闭速度
void setCloseTimeSpeed(int closeTime = 100,double closeSpeed = 0.1);
//设置边框颜色
void setFrameColor(const QColor &value);
//设置边框粗细
void setFrameSize(int value);
//设置透明度
void setOpacityValue(double value);
protected:
void paintEvent(QPaintEvent *event) override;
private:
void InitLayout();//初始化窗体的布局和部件
QHBoxLayout *hBoxlayout;//布局显示控件布局
QLabel *mText;//用于显示文字的控件
QString showStr;//显示的字符串
QColor backgroundColor;//窗体的背景色
QColor textColor;//字体颜色
int textSize;//显示字体大小
int showTime;//显示时间
int closeTime;//关闭需要时间
double closeSpeed;//窗体消失的平滑度,大小0~1
QColor frameColor;//边框颜色
int frameSize;//边框粗细大小
double opacityValue;//窗体初始化透明度
};
#endif // MESSAGETIPS_H
messagetips.cpp
#include "messagetips.h"
#include <QDesktopWidget>
#include <QHBoxLayout>
#include <QLabel>
#include <QPainter>
#include <QTimer>
#include<QApplication>
#include<QDebug>
MessageTips::MessageTips(QString showStr,QWidget *parent) : QWidget(parent),
hBoxlayout(new QHBoxLayout(this)),
mText(new QLabel(this)),
backgroundColor(QColor(120,120,120)),
textColor(QColor(255,255,255)),
textSize(14),
showTime(1000),
closeTime(100),
closeSpeed(0.1),
frameColor(QColor(0,0,0,0)),
frameSize(0),
opacityValue(1.0)
{
setWindowFlags(Qt::Window|Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint|Qt::Tool|Qt::X11BypassWindowManagerHint);
this->setAttribute(Qt::WA_TranslucentBackground); // ****这里很重要****
this->setAttribute(Qt::WA_TransparentForMouseEvents, true);// 禁止鼠标事件
this->setAttribute(Qt::WA_DeleteOnClose,true);//关闭窗口自动调用析构函数
this->showStr = showStr;
hBoxlayout->addWidget(mText);
InitLayout();
}
MessageTips::~MessageTips()
{
if (hBoxlayout != NULL)
{
delete hBoxlayout;
hBoxlayout = NULL;
}
if (mText != NULL)
{
delete mText;
mText = NULL;
}
}
void MessageTips::InitLayout()
{
this->setWindowOpacity(opacityValue);
//文字显示居中,设置字体,大小,颜色
QFont font = QFont("微软雅黑",textSize,QFont::Bold);
mText->setStyleSheet("QLabel{background-color:transparent;}");
mText->setFont(font);
mText->setText(showStr);
mText->adjustSize();
mText->setAlignment(Qt::AlignCenter);
QPalette label_pe;//设置字体颜色
label_pe.setColor(QPalette::WindowText, textColor);
mText->setPalette(label_pe);
QTimer *mtimer = new QTimer(this);//隐藏的定时器
mtimer->setTimerType(Qt::PreciseTimer);
connect(mtimer,&QTimer::timeout,this,[=](){
if(opacityValue<=0){ this->close(); }
opacityValue = opacityValue-closeSpeed;
this->setWindowOpacity(opacityValue); //设置窗口透明度
});
QTimer *mShowtimer = new QTimer(this);//显示时间的定时器
mShowtimer->setTimerType(Qt::PreciseTimer);// 修改定时器对象的精度
connect(mShowtimer,&QTimer::timeout,this,[=](){
mtimer->start(closeTime);//执行延时自动关闭
mtimer->destroyed();
mShowtimer->deleteLater();
});
mShowtimer->start(showTime);
//设置屏幕居中显示
QDesktopWidget* desktop = QApplication::desktop(); // =qApp->desktop();也可以
this->move((desktop->width() - mText->width())/2, (desktop->height() - mText->height())*7/8);
this->setAttribute(Qt::WA_TransparentForMouseEvents, true);// 禁止鼠标事件
}
void MessageTips::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QPainter painter(this);
painter.setBrush(QBrush(backgroundColor));//窗体的背景色
painter.setPen(QPen(frameColor,frameSize));//窗体边框的颜色和笔画大小
QRectF rect(0, 0, this->width(), this->height());
painter.drawRoundedRect(rect, 15, 15); // round rect
}
void MessageTips::setBackgroundColor(const QColor &value)
{
backgroundColor = value;
}
void MessageTips::setTextColor(const QColor &value)
{
textColor = value;
InitLayout();
}
void MessageTips::setTextSize(int value)
{
textSize = value;
InitLayout();
}
void MessageTips::setShowTime(int value)
{
showTime = value;
InitLayout();
}
void MessageTips::setCloseTimeSpeed(int closeTime, double closeSpeed)
{
if(closeSpeed>0 && closeSpeed<=1){
this->closeSpeed = closeSpeed;
}
this->closeTime = closeTime;
InitLayout();
}
void MessageTips::setFrameColor(const QColor &value)
{
frameColor = value;
}
void MessageTips::setFrameSize(int value)
{
frameSize = value;
}
void MessageTips::setOpacityValue(double value)
{
opacityValue = value;
InitLayout();
}