思路:弹出一个无边框窗体,再添加一个QLabel显示消息,使用了两个定时器,一个控制显示时间,一个控制缓慢消失的时间(没过一个时间,把透明度-.0.1,当透明度=0,关闭窗体)
效果:
主窗体调用:
void MainWindow::on_pushButton_clicked()
{
MessageTips *mMessageTips = new MessageTips("网络连接失败,正在重新连接中 . . .",this);
mMessageTips->show();
}
把这个弹出框封装成一个类,可以直接调用:
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),
opacityValue(1.0),
textSize(18),
textColor(QColor(255,255,255)),
backgroundColor(QColor(192,192,192)),
frameColor(QColor(211,211,211)),
frameSize(2),
showTime(3500),
closeTime(100),
closeSpeed(0.1),
hBoxlayout(new QHBoxLayout(this)),
mText(new QLabel(this))
{
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);
this->showStr = showStr;
hBoxlayout->addWidget(mText);
InitLayout();
}
void MessageTips::InitLayout()
{
this->setWindowOpacity(opacityValue);
//文字显示居中,设置字体,大小,颜色
font = new QFont("微软雅黑",textSize,QFont::Bold);
mText->setFont(*font);
mText->setText(showStr);
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){
mtimer->stop();
this->close();
return;
}
opacityValue = opacityValue-closeSpeed;
this->setWindowOpacity(opacityValue); //设置窗口透明度
});
QTimer::singleShot(showTime,[=](){mtimer->start(closeTime);});//执行延时自动关闭
//设置屏幕居中显示
QDesktopWidget* desktop = QApplication::desktop(); // =qApp->desktop();也可以
this->move((desktop->width() - this->width())/2, (desktop->height() - this->height())/2);
this->setAttribute(Qt::WA_TransparentForMouseEvents, true);// 禁止鼠标事件
}
void MessageTips::paintEvent(QPaintEvent *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
}
//设置关闭的时间和速度,speed大小限定0~1
void MessageTips::setCloseTimeSpeed(int closeTime, double closeSpeed)
{
if(closeSpeed>0 && closeSpeed<=1){
this->closeSpeed = closeSpeed;
}
this->closeTime = closeTime;
InitLayout();
}
int MessageTips::getShowTime() const
{
return showTime;
}
void MessageTips::setShowTime(int value)
{
showTime = value;
InitLayout();
}
int MessageTips::getFrameSize() const
{
return frameSize;
}
void MessageTips::setFrameSize(int value)
{
frameSize = value;
}
QColor MessageTips::getFrameColor() const
{
return frameColor;
}
void MessageTips::setFrameColor(const QColor &value)
{
frameColor = value;
}
QColor MessageTips::getBackgroundColor() const
{
return backgroundColor;
}
void MessageTips::setBackgroundColor(const QColor &value)
{
backgroundColor = value;
}
QColor MessageTips::getTextColor() const
{
return textColor;
}
void MessageTips::setTextColor(const QColor &value)
{
textColor = value;
InitLayout();
}
int MessageTips::getTextSize() const
{
return textSize;
}
void MessageTips::setTextSize(int value)
{
textSize = value;
InitLayout();
}
double MessageTips::getOpacityValue() const
{
return opacityValue;
}
void MessageTips::setOpacityValue(double value)
{
opacityValue = value;
InitLayout();
}
messagetips.h文件
#ifndef MESSAGETIPS_H
#define MESSAGETIPS_H
#include <QWidget>
# pragma execution_character_set("utf-8")
class QHBoxLayout;
class QLabel;
class MessageTips : public QWidget
{
Q_OBJECT
public:
explicit MessageTips(QString showStr="默认显示", QWidget *parent = nullptr);
double getOpacityValue() const;
void setOpacityValue(double value);
int getTextSize() const;
void setTextSize(int value);
QColor getTextColor() const;
void setTextColor(const QColor &value);
QColor getBackgroundColor() const;
void setBackgroundColor(const QColor &value);
QColor getFrameColor() const;
void setFrameColor(const QColor &value);
int getFrameSize() const;
void setFrameSize(int value);
int getShowTime() const;
void setShowTime(int value);
void setCloseTimeSpeed(int closeTime = 100,double closeSpeed = 0.1);
protected:
void paintEvent(QPaintEvent *event) override;
private:
void InitLayout();//初始化窗体的布局和部件
QHBoxLayout *hBoxlayout;//布局显示控件布局
QLabel *mText;//用于显示文字的控件
QString showStr;//显示的字符串
double opacityValue;//窗体初始化透明度
QFont *font;
int textSize;//显示字体大小
QColor textColor;//字体颜色
QColor backgroundColor;//窗体的背景色
QColor frameColor;//边框颜色
int frameSize;//边框粗细大小
int showTime;//显示时间
int closeTime;//关闭需要时间
double closeSpeed;//窗体消失的平滑度,大小0~1
signals:
};
#endif // MESSAGETIPS_H
不足之处,请告知