QT realizes that the pop-up prompt box slowly fades and disappears

Refer to the original text: qt message pop-up box, no frame, disappears slowly and automatically

The code was modified based on the original author’s instructions, and bugs such as the prompt box not being centered horizontally, the color modification being invalid due to the influence of the parent class during use, and the timer not being destroyed were fixed, and the code was recoded according to my own coding habits.

Applicable version: Qt5

Effect demonstration:

Main form call:

void Widget::on_pushButton_clicked()
{
    MessageTips *mTips=new MessageTips("I hid in the crowd and lost the clear sky");
    mTips->show();
}

This is different from the original calling method. This is no longer inherited (otherwise it will be affected by the style sheet of the parent class and cause operations such as color modification to fail). Instead, the destructor is automatically called when the window is closed in the constructor of 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="Default display", QWidget *parent = nullptr);
    ~MessageTips();
    //Set background color
    void setBackgroundColor(const QColor & amp;value);
    //Set text color
    void setTextColor(const QColor & amp;value);
    //Set text font size
    void setTextSize(int value);
    //Set display time
    void setShowTime(int value);
    //Set closing speed
    void setCloseTimeSpeed(int closeTime = 100,double closeSpeed = 0.1);
    //Set border color
    void setFrameColor(const QColor & amp;value);
    //Set the border thickness
    void setFrameSize(int value);
    //Set transparency
    void setOpacityValue(double value);

protected:
    void paintEvent(QPaintEvent *event) override;

private:
    void InitLayout();//Initialize the layout and components of the form
    QHBoxLayout *hBoxlayout;//Layout display control layout
    QLabel *mText;//Control used to display text
    QString showStr; //Displayed string

    QColor backgroundColor;//Background color of the form
    QColor textColor;//Font color
    int textSize;//Display font size
    int showTime;//display time
    int closeTime;//It takes time to close
    double closeSpeed;//The smoothness of the form disappearing, size 0~1
    QColor frameColor;//border color
    int frameSize;//border thickness size
    double opacityValue;//form initialization transparency
};

#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 is very important****
    this->setAttribute(Qt::WA_TransparentForMouseEvents, true);//Disable mouse events
    this->setAttribute(Qt::WA_DeleteOnClose,true);//Close the window and automatically call the destructor
    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);

    //Text display is centered, set font, size, color
    QFont font = QFont("Microsoft Yahei",textSize,QFont::Bold);
    mText->setStyleSheet("QLabel{background-color:transparent;}");
    mText->setFont(font);
    mText->setText(showStr);
    mText->adjustSize();
    mText->setAlignment(Qt::AlignCenter);
    QPalette label_pe;//Set font color
    label_pe.setColor(QPalette::WindowText, textColor);
    mText->setPalette(label_pe);

    QTimer *mtimer = new QTimer(this);//Hidden timer
    mtimer->setTimerType(Qt::PreciseTimer);
    connect(mtimer, & amp;QTimer::timeout,this,[=](){
        if(opacityValue<=0){ this->close(); }
        opacityValue = opacityValue-closeSpeed;
        this->setWindowOpacity(opacityValue); //Set window transparency
        });

    QTimer *mShowtimer = new QTimer(this);//Timer that displays time
    mShowtimer->setTimerType(Qt::PreciseTimer);//Modify the precision of the timer object
    connect(mShowtimer, & amp;QTimer::timeout,this,[=](){
        mtimer->start(closeTime);//Execution delay automatically closes
        mtimer->destroyed();
        mShowtimer->deleteLater();
        });
    mShowtimer->start(showTime);

    //Set the screen to be centered
    QDesktopWidget* desktop = QApplication::desktop(); // =qApp->desktop(); can also
    this->move((desktop->width() - mText->width())/2, (desktop->height() - mText->height())*7/8);
    this->setAttribute(Qt::WA_TransparentForMouseEvents, true);//Disable mouse events
}

void MessageTips::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter painter(this);
    painter.setBrush(QBrush(backgroundColor));//Background color of the form

    painter.setPen(QPen(frameColor,frameSize));//Color and stroke size of the form border
    QRectF rect(0, 0, this->width(), this->height());
    painter.drawRoundedRect(rect, 15, 15); // round rect
}

void MessageTips::setBackgroundColor(const QColor & amp;value)
{
    backgroundColor = value;
}

void MessageTips::setTextColor(const QColor & amp;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 & amp; & amp; closeSpeed<=1){
       this->closeSpeed = closeSpeed;
    }
   this->closeTime = closeTime;
    InitLayout();
}

void MessageTips::setFrameColor(const QColor & amp;value)
{
    frameColor = value;
}

void MessageTips::setFrameSize(int value)
{
    frameSize = value;
}

void MessageTips::setOpacityValue(double value)
{
    opacityValue = value;
    InitLayout();
}