QT 消息弹出后 消失

1.创建对象;
 toast_class toast;
2.发出弹窗消息
 toast.showTip(tr("Camera disconnected"),  nullptr);

3.toast 类的头文件,也是继承 QWidget

#ifndef TOAST_CLASS_H
#define TOAST_CLASS_H

#include <QLabel>
#include <QWidget>



class toast_class : public QWidget
{
    Q_OBJECT
public:
    explicit toast_class(QWidget *parent = nullptr);
    ~toast_class();
    void setText(const QString &text);
    void showAnimation(int timeout = 2000);

public:
    static void showTip(const QString &text, QWidget *parent = nullptr);
    static void showTip(const QString &text, int timeout, QWidget *parent = nullptr);

protected:
    virtual void paintEvent(QPaintEvent *event);
};
#endif // TOAST_CLASS_H

4.toast_class 也是在窗体上创建一个label 

QLabel *label = new QLabel(this);
    label->setObjectName("labelText");
    label->setStyleSheet("background-color: rgba(255,0,0,0.80);"  //背景颜色                                               "border-radius: 16px;"                    //圆角半径
                         "color: #FFFFFF;"                        //字体颜色
                         "font-family: microsoft yahei;"
                         "font-size: 30px;"
                         "padding-left:25px;"
                         "padding-right:25px;");
    label ->setAlignment(Qt::AlignCenter);//居中
    label->setWordWrap(true);
    layout->addWidget(label);
    setLayout(layout);//放入布局中


void toast_class::showAnimation(int timeout)
{
    /* 快进慢出 */
    show();

    QTimer::singleShot(timeout, [&]
    {
        /* 结束动画 */
        QPropertyAnimation *animation = new QPropertyAnimation(this, "windowOpacity");
        animation->setDuration(1000);  //动画持续时间
        animation->setStartValue(1);
        animation->setEndValue(0);
        animation->start();
        connect(animation, &QPropertyAnimation::finished, [&]
        {
            close();
            deleteLater();
        });  //lambda表达式
    });  //定时器超时
}

#include "toast_class.h"

#include <QGuiApplication>
#include <QPainter>
#include <QPropertyAnimation>
#include <QScreen>
#include <QTimer>
#include <QVBoxLayout>

toast_class::toast_class(QWidget *parent)
    : QWidget(parent)
{
    setWindowFlags(windowFlags() | Qt::FramelessWindowHint | Qt::Tool);
    setAttribute(Qt::WA_TranslucentBackground, true);
    QVBoxLayout *layout = new QVBoxLayout;
    QLabel *label = new QLabel(this);
    label->setObjectName("labelText");
    label->setStyleSheet("background-color: rgba(255,0,0,0.80);"
                         "border-radius: 16px;"
                         "color: #FFFFFF;"
                         "font-family: microsoft yahei;"
                         "font-size: 30px;"
                         "padding-left:25px;"
                         "padding-right:25px;");
    label ->setAlignment(Qt::AlignCenter);
    label->setWordWrap(true);

    layout->addWidget(label);
    setLayout(layout);
}

toast_class::~toast_class()
{

}

void toast_class::setText(const QString &text)
{
    QLabel *label = this->findChild<QLabel *>("labelText");
    label->setText(text);
}

void toast_class::showAnimation(int timeout)
{
    /* 快进慢出 */
    show();

    QTimer::singleShot(timeout, [&]
    {
        /* 结束动画 */
        QPropertyAnimation *animation = new QPropertyAnimation(this, "windowOpacity");
        animation->setDuration(1000);
        animation->setStartValue(1);
        animation->setEndValue(0);
        animation->start();
        connect(animation, &QPropertyAnimation::finished, [&]
        {
            close();
            deleteLater();
        });
    });
}

void toast_class::showTip(const QString &text, QWidget *parent)
{
    toast_class *toast = new toast_class(parent);
    toast->setWindowFlags(toast->windowFlags() | Qt::WindowStaysOnTopHint); /* 置顶 */
    toast->setText(text);
    toast->adjustSize();


    QScreen *pScreen = QGuiApplication::primaryScreen();
    toast->move((pScreen->size().width() - toast->width()) / 2, pScreen->size().height() * 0.4);
    toast->showAnimation();
}

void toast_class::showTip(const QString &text, int timeout, QWidget *parent)
{
    toast_class *toast = new toast_class(parent);
    toast->setWindowFlags(toast->windowFlags() | Qt::WindowStaysOnTopHint);
    toast->setText(text);
    toast->adjustSize();

    QScreen *pScreen = QGuiApplication::primaryScreen();
    toast->move((pScreen->size().width() - toast->width()) / 2, pScreen->size().height() * 0.4);
    toast->showAnimation(timeout);
}

void toast_class::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)
    QPainter paint(this);
    auto kBackgroundColor = QColor(255, 255, 255);
    kBackgroundColor.setAlpha(0.0 * 255);
    paint.setPen(Qt::NoPen);
    paint.setBrush(QBrush(kBackgroundColor, Qt::SolidPattern));
    paint.drawRect(0, 0, width(), height());
}

5.效果:

会渐渐变暗,慢慢消失

  • 19
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chilian12321

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值