QT基础 QPropertyAnimation简单学习

 

目录

 

1.简单介绍

 2.使用步骤

 3.部分代码示例

4.多项说明 

5.信号反馈

6.自定义属性

1. 定义自定义属性

2. 使用 QPropertyAnimation 动画化自定义属性

3. 连接信号和槽

4.注意事项

7.更多高级示例


1.简单介绍

QPropertyAnimation是Qt中的一个类,用于实现属性动画效果。它通过改变对象的属性值来创建动画效果,可以实现平移、旋转、缩放等动画效果。QPropertyAnimation继承自QAbstractAnimation类,因此可以和其他动画类一起使用,同时也可以结合QAnimationGroup来实现多个动画的组合效果。

QPropertyAnimation的核心是通过插值器(Interpolator)和时间线(Timeline)来控制属性值的变化。插值器决定了属性值在时间线上的变化规律,而时间线则决定了动画的时长和速度。

QPropertyAnimation可以作用于任何QObject的派生类,包括QWidget、QGraphicsItem等。它可以控制对象的任何可变属性,只要属性可以通过setter和getter方法来获取和设置。

使用QPropertyAnimation可以轻松地为应用程序添加动画效果,提升用户体验。无论是在桌面应用程序还是移动应用程序中,都可以使用QPropertyAnimation来创建各种各样的动画效果。

 2.使用步骤

使用 QPropertyAnimation 的基本步骤:

  1. 创建动画对象:首先,你需要创建一个 QPropertyAnimation 对象,并将其与要动画化的属性相关联。

  2. 设置目标对象和属性:你需要指定动画的目标对象和要动画化的属性。

  3. 设置动画参数:包括动画的起始值、结束值、持续时间、缓动函数等。

  4. 启动动画:最后,通过调用 start() 方法来启动动画。

 3.部分代码示例

void MainWindow::initUi()
{
    setWindowFlags(windowFlags() | Qt::FramelessWindowHint);

    connect(ui->pushButton, &QPushButton::clicked,this, &MainWindow::slotsclicked);
}

void MainWindow::slotsclicked()
{
    static int resizeValue{100};
    resizeValue = -resizeValue;
    QPropertyAnimation* animation = new QPropertyAnimation(this, "geometry");
    animation->setDuration(500);  // 持续时间
    
    //运动轨迹
    animation->setStartValue(QRect(pos().x(), pos().y(), width(), height()));
    //animation->setEndValue(QRect(pos().x() + resizeValue, pos().y() + resizeValue, width(), height()));
    animation->setEndValue(QRect(pos().x() + width(), pos().y(), 0, height()));
    
    animation->setEasingCurve(QEasingCurve::InQuad);
    animation->start(QAbstractAnimation::DeleteWhenStopped);
}

4.多项说明 

1.关于setEasingCurve其实有更多的运动类型,可以搜索QEasingCurve

QPropertyAnimation *animation = new QPropertyAnimation(&object, "propertyName");

在这个例子中,"propertyName" 应该替换为你想要动画化的属性的实际名称。object 是动画的目标对象,它应该是一个 QObject 的实例。

Qt 使用属性系统来管理对象的状态,每个属性都有一个名称,可以通过 QObject::setProperty()QObject::property() 方法来设置和获取。QPropertyAnimation 利用这个系统来动态地改变属性值。

   for(int i=0;i<ui->pushButton->metaObject()->propertyCount();i++)
        qDebug()<<ui->pushButton->metaObject()->property(i).name();

下面是一些常见的属性名称示例:

  • "geometry":用于动画化 QWidget 或其子类对象的几何形状。
  • "pos":用于动画化 QWidget 或其子类对象的位置。
  • "size":用于动画化 QWidget 或其子类对象的大小。
  • "x""y":用于动画化 QWidget 或其子类对象在父对象坐标系中的 x 或 y 坐标。
  • "opacity":用于动画化 QWidget 或其子类对象的不透明度。

 上面提到要动画化的属性相关联,如果上面代码修改成opacity,就无法工作

5.信号反馈

QPropertyAnimation 中,你可以监听以下几个回调函数来处理动画完成后的事件:

  1. finished 信号:当动画完成时发出。这个信号不传递任何参数。

    connect(animation, &QPropertyAnimation::finished, this, &MyClass::onAnimationFinished);

    MyClass::onAnimationFinished 槽函数中,你可以执行动画完成后需要进行的操作。

  2. valueChanged 信号:每当动画的当前值改变时发出。这个信号传递一个 QVariant 参数,表示动画的当前值。

    connect(animation, &QPropertyAnimation::valueChanged, this, &MyClass::onValueChanged);

    MyClass::onValueChanged 槽函数中,你可以获取动画的当前值并根据需要进行处理。

  3. started 信号:当动画开始时发出。这个信号不传递任何参数。

    connect(animation, &QPropertyAnimation::started, this, &MyClass::onAnimationStarted);

    MyClass::onAnimationStarted 槽函数中,你可以执行动画开始时需要进行的操作。

  4. stopped 信号:当动画被停止时发出。这个信号不传递任何参数。

    connect(animation, &QPropertyAnimation::stopped, this, &MyClass::onAnimationStopped);

    MyClass::onAnimationStopped 槽函数中,你可以执行动画停止时需要进行的操作。

  5. stateChanged 信号:当动画的状态改变时发出。这个信号传递两个参数:QAbstractAnimation::State 表示新状态,QAbstractAnimation::State 表示旧状态。

    connect(animation, &QPropertyAnimation::stateChanged, this, &MyClass::onAnimationStateChanged);

    MyClass::onAnimationStateChanged 槽函数中,你可以根据动画的新旧状态执行相应的操作。

这些信号允许你在动画的不同阶段执行自定义的逻辑,例如更新用户界面、处理动画完成后的清理工作或者触发其他动画。

6.自定义属性

Qt中,如果你想要动画化一个不是由Qt框架预定义的属性,你可以使用 QVariant 属性系统来自定义属性。自定义属性允许你为任何 QObject 派生类添加新的属性,并通过 QPropertyAnimation 来动画化这些属性。

以下是如何为自定义类添加属性并使用 QPropertyAnimation 来动画化这些属性的步骤:

1. 定义自定义属性

首先,你需要在你的类定义中使用 Q_PROPERTY 宏来声明自定义属性。这通常在类的头文件中完成。

#include <QObject>
#include <QVariant>

class MyClass : public QObject {
    Q_OBJECT
    Q_PROPERTY(int customInt READ customInt WRITE setCustomInt NOTIFY customIntChanged)
    Q_PROPERTY(QColor customColor READ customColor WRITE setCustomColor NOTIFY customColorChanged)

public:
    MyClass(QObject *parent = nullptr) : QObject(parent) {}

    int customInt() const { return m_customInt; }
    void setCustomInt(int value) { m_customInt = value; emit customIntChanged(value); }

    QColor customColor() const { return m_customColor; }
    void setCustomColor(const QColor &value) { m_customColor = value; emit customColorChanged(value); }

signals:
    void customIntChanged(int newValue);
    void customColorChanged(const QColor &newValue);

private:
    int m_customInt;
    QColor m_customColor;
};

2. 使用 QPropertyAnimation 动画化自定义属性

一旦你定义了自定义属性,你可以像动画化任何其他属性一样动画化它们。

#include <QApplication>
#include <QPropertyAnimation>

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    MyClass myObject;
    QPropertyAnimation *animation = new QPropertyAnimation(&myObject, "customInt");
    animation->setStartValue(0);
    animation->setEndValue(100);
    animation->setDuration(2000);
    animation->start();

    return app.exec();
}

3. 连接信号和槽

你还可以连接自定义属性的更改信号到槽函数,以便在属性值改变时执行某些操作。

connect(&myObject, &MyClass::customIntChanged, [](int newValue) {
    qDebug() << "Custom int changed to:" << newValue;
});

4.注意事项

  • 确保你的自定义属性类型是 QVariant 可以存储的类型。对于不支持的类型,你可能需要使用 QVariant 来包装它们。
  • 使用 Q_PROPERTY 宏时,确保你提供了 READWRITE 和 NOTIFY 宏,这样Qt的属性系统才能正确地处理它们。

7.更多高级示例

示例1    示例2   示例3

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

路奇怪

有钱出钱,没钱多出编程主意啊

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

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

打赏作者

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

抵扣说明:

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

余额充值