【Qt 学习之路】QPropertyAnimation开幕动画

目录

1、效果

2、核心源码

3、QPropertyAnimation探究

3.1、官方示例

3.2、属性成员

3.3、成员函数


1、效果


2、核心源码

#include <QPropertyAnimation>

QPropertyAnimation *m_leftAnimation;
QPropertyAnimation *m_rightAnimation;

m_leftAnimation = new QPropertyAnimation(ui->lab_left,"pos");
m_rightAnimation  = new QPropertyAnimation(ui->lab_right,"pos");

int duration = 2000;
m_leftAnimation->setDuration(duration);
m_rightAnimation->setDuration(duration);
m_leftAnimation->setStartValue(QPoint(0,0));
m_leftAnimation->setEndValue(QPoint(-width/2,0));
m_rightAnimation->setStartValue(QPoint(width/2,0));
m_rightAnimation->setEndValue(QPoint(width,0));
m_leftAnimation->setEasingCurve(QEasingCurve::InOutCirc);
m_rightAnimation->setEasingCurve(QEasingCurve::InOutCirc);
m_leftAnimation->start();
m_rightAnimation->start();

3、QPropertyAnimation探究

QPropertyAnimation 对 Qt 属性进行插值。由于属性值存储在 QVariant 中,该类继承了 QVariantAnimation,并支持与其超类相同元类型的动画。

声明属性的类必须是 QObject。为了能够为属性设置动画,它必须提供一个 setter(以便 QPropertyAnimation 可以设置属性的值)。这使得为许多 Qt 小部件制作动画成为可能

3.1、官方示例

    QPropertyAnimation *animation = new QPropertyAnimation(myWidget, "geometry");
    animation->setDuration(10000);
    animation->setStartValue(QRect(0, 0, 100, 30));
    animation->setEndValue(QRect(250, 250, 100, 30));
 
    animation->start();

3.2、属性成员

1、propertyName : QByteArray

此属性定义动画的目标属性名称。动画运行需要属性名称。

2、targetObject : QObject*

此属性定义动画的目标 QObject。

3.3、成员函数

1、QPropertyAnimation(QObject *target, const QByteArray &propertyName, QObject *parent = nullptr)

构造一个 QPropertyAnimation 对象。parent 传递给 QObject 的构造函数。动画更改目标上的属性 propertyName。默认持续时间为 250 毫秒。

2、void updateCurrentValue(const QVariant &value)

每当当前值发生变化时,都会调用这个虚函数。value 是更新的值。它更新目标对象上属性的当前值。

3、void updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState)

如果在动画状态从 Stopped 变为 Running 时未定义 startValue,则将当前属性值用作动画的初始值。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

沙振宇

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

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

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

打赏作者

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

抵扣说明:

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

余额充值