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.效果:
会渐渐变暗,慢慢消失