QT中的QPropertyAnimation使用和toast案列

本文介绍了如何利用Qt的QPropertyAnimation类创建类似toast的通知效果。通过设置控件的移动、缩放和不透明度属性,实现toast在屏幕上的动画展示。文章提供了一个自定义的Toast类,包括其头文件和源码实现,展示了如何在主窗口中调用并显示toast提示。此外,还提供了详细的代码示例,帮助读者理解如何应用QPropertyAnimation实现自定义动画效果。
摘要由CSDN通过智能技术生成


0 引入

最近使用学习到动画这一块,无意中接触到使用QPropertyAnimation来完成 类似 toast效果的东西


在这里插入图片描述
主要功能:
1、toast实现位置可以根据父窗口而改变;
2、显示时间默认是一分钟,可调

1、QPropertyAnimation自带效果

QPropertyAnimation是Qt自带的动画类,该类可以实现简单的控件动画效果,比如对控件的移动、缩放、不透明度这些来做动画效果(使用某效果之前需要使用setPropertyName函数指定需要的动画属性名,以下三个是Qt已经定义好的)。

1、移动(pos):主要实现移动效果,如从某个点移动到另一个点,所使用的变量类型为QPoint等。
2、缩放(geometry):可实现缩放和移动效果,该属性可以实现指定控件的缩放,并且在缩放的基础上还能实现移动。
3、不透明度(windowOpacity):实现控件的透明度设置(不过该属性只能对顶级窗口使用,对普通控件这些无效)。


2、QPropertyAnimation自定义效果

见引用。

3、QPropertyAnimation toast实现

1.toast头文件

代码如下(示例):

#ifndef TOAST_H
#define TOAST_H

#include <QtWidgets/QWidget>
#include <QLabel>


class Toast : public QWidget
{
    Q_OBJECT
public:
    Toast(QWidget *parent = Q_NULLPTR);
    ~Toast();

    void setText(const QString& text);
    void showAnimation(int timeout = 500);
    void movetoPosition(int x,int y);
    static void showTip(const QString& text,int x,int y,QWidget* parent = nullptr);

private:
    QLabel *label = nullptr;
    QWidget *widget = nullptr;
};

#endif // TOAST_H

2.toast cpp

代码如下(示例):

#include "toast.h"
#include <QPropertyAnimation>
#include <QScreen>
#include <QGuiApplication>
#include <QPainter>
#include <QTimer>
#include <QDebug>
#include <QGridLayout>

Toast::Toast(QWidget *parent)
    : QWidget(parent)
{

    label = new QLabel();
    label->setStyleSheet("background-color: rgba(0,0,0,0.80);\nborder-radius: 26px;\ncolor: #FFFFFF;\nfont-family: microsoft yahei;\nfont-size: 16px;\npadding-left:25px;\npadding-right:25px;");
    QGridLayout *layout = new QGridLayout;
    layout->addWidget(label);

    widget = new QWidget();
    widget->setWindowFlags(windowFlags() | Qt::FramelessWindowHint | Qt::Tool);
    widget->setAttribute(Qt::WA_TranslucentBackground, true);

    widget->setGeometry(0,0,170,52);
    widget->setLayout(layout);

    QScreen* pScreen = QGuiApplication::primaryScreen();
    widget->move((pScreen->size().width() - widget->width()) / 2, pScreen->size().height() /2);
    widget->setVisible(true);
}

Toast::~Toast()
{
    delete label;
    delete widget;
}

void Toast::setText(const QString& text)
{
    label->setText(text);
}

void Toast::showAnimation(int timeout)
{
    QTimer::singleShot(timeout, [&]
    {
        QPropertyAnimation *animation = new QPropertyAnimation(this, "windowOpacity");
        animation->setDuration(500);
        animation->setStartValue(1);
        animation->setEndValue(0);
        animation->start();
        connect(animation, &QPropertyAnimation::finished, [&]
        {
            close();
            deleteLater();
        });
    });
}

void Toast::movetoPosition(int x,int y)
{
    widget->move(x,y);
}

void Toast::showTip(const QString& text ,int x,int y,QWidget* parent /*= nullptr*/)
{
    Toast* toast = new Toast(parent);
    toast->setWindowFlags(toast->windowFlags() | Qt::WindowStaysOnTopHint);
    toast->setText(text);
    toast->adjustSize();
    toast->movetoPosition(x, y);
    toast->showAnimation();
}

3.main cpp

void MainWindow::on_pushButton_4_clicked()
{

   Toast::showTip("测试成功!测试成功!测试成功!测试成功!测试成功!",this->x(),this->y());
}


void MainWindow::on_pushButton_5_clicked()
{
    qDebug()<<"12";
 //   Toast::showTip(QApplication::translate("Dialog", "Please set destination IP !"));
     Toast::showTip("测试成功!",this->x(),this->y());
    qDebug()<<"123";
}void MainWindow::on_pushButton_4_clicked()
{

   Toast::showTip("测试成功!测试成功!测试成功!测试成功!测试成功!",this->x(),this->y());
}


4、总结

文章参考引用的内容,然后自己实现一个toast,只需要包含头文件和cpp就可以在程序中使用了


5、引用

1、QT自定义属性动画
2、QT实现toast效果


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

疯狂的挖掘机

谢谢大家的厚爱

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

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

打赏作者

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

抵扣说明:

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

余额充值