QPropertyAnimation Class

The QPropertyAnimation class animates Qt properties. 更多...

头: #include <QPropertyAnimation>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core
继承: QVariantAnimation

特性

公共函数

QPropertyAnimation (QObject * parent = nullptr)
QPropertyAnimation (QObject * target , const QByteArray & propertyName , QObject * parent = nullptr)
virtual ~QPropertyAnimation ()
QBindable<QByteArray> bindablePropertyName ()
QBindable<QObject *> bindableTargetObject ()
QByteArray propertyName () const
void setPropertyName (const QByteArray & propertyName )
void setTargetObject (QObject * target )
QObject * targetObject () const

重实现保护函数

virtual bool event (QEvent * event ) override
virtual void updateCurrentValue (const QVariant & value ) override
virtual void updateState (QAbstractAnimation::State newState , QAbstractAnimation::State oldState ) override

详细描述

QPropertyAnimation interpolates over Qt properties . As property values are stored in QVariant s, the class inherits QVariantAnimation , and supports animation of the same meta types as its super class.

A class declaring properties must be a QObject . To make it possible to animate a property, it must provide a setter (so that QPropertyAnimation can set the property's value). Note that this makes it possible to animate many of Qt's widgets. Let's look at an example:

#include <QApplication>
#include <QPushButton>
#include <QPropertyAnimation>
class MyButtonWidget : public QWidget
{
public:
    MyButtonWidget(QWidget *parent = nullptr);
};
MyButtonWidget::MyButtonWidget(QWidget *parent) : QWidget(parent)
{
    QPushButton *button = new QPushButton(tr("Animated Button"), this);
    QPropertyAnimation *anim = new QPropertyAnimation(button, "pos", this);
    anim->setDuration(10000);
    anim->setStartValue(QPoint(0, 0));
    anim->setEndValue(QPoint(100, 250));
    anim->start();
}
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MyButtonWidget buttonAnimWidget;
    buttonAnimWidget.resize(QSize(800, 600));
    buttonAnimWidget.show();
    return a.exec();
}
					

注意: You can also control an animation's lifespan by choosing a 删除策略 while starting the animation.

The property name and the QObject instance of which property should be animated are passed to the constructor. You can then specify the start and end value of the property. The procedure is equal for properties in classes you have implemented yourself–just check with QVariantAnimation that your QVariant type is supported.

The QVariantAnimation class description explains how to set up the animation in detail. Note, however, that if a start value is not set, the property will start at the value it had when the QPropertyAnimation instance was created.

QPropertyAnimation works like a charm on its own. For complex animations that, for instance, contain several objects, QAnimationGroup is provided. An animation group is an animation that can contain other animations, and that can manage when its animations are played. Look at QParallelAnimationGroup 范例。

另请参阅 QVariantAnimation , QAnimationGroup ,和 动画框架 .

特性文档编制

[bindable] propertyName : QByteArray

注意: 此特性支持 QProperty 绑定。

This property holds the target property name for this animation

This property defines the target property name for this animation. The property name is required for the animation to operate.

[bindable] targetObject : QObject *

注意: 此特性支持 QProperty 绑定。

This property holds the target QObject for this animation.

This property defines the target QObject for this animation.

成员函数文档编制

QPropertyAnimation:: QPropertyAnimation ( QObject * parent = nullptr)

Construct a QPropertyAnimation object. parent 会被传递给 QObject 的构造函数。

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

Construct a QPropertyAnimation object. parent 会被传递给 QObject 's constructor. The animation changes the property propertyName on target . The default duration is 250ms.

另请参阅 targetObject and propertyName .

[virtual noexcept] QPropertyAnimation:: ~QPropertyAnimation ()

销毁 QPropertyAnimation 实例。

[override virtual protected] bool QPropertyAnimation:: event ( QEvent * event )

重实现: QVariantAnimation::event (QEvent *event).

[override virtual protected] void QPropertyAnimation:: updateCurrentValue (const QVariant & value )

重实现: QVariantAnimation::updateCurrentValue (const QVariant &value).

此虚函数被调用由 QVariantAnimation whenever the current value changes. value is the new, updated value. It updates the current value of the property on the target object.

另请参阅 currentValue and currentTime .

[override virtual protected] void QPropertyAnimation:: updateState ( QAbstractAnimation::State newState , QAbstractAnimation::State oldState )

重实现: QVariantAnimation::updateState (QAbstractAnimation::State newState, QAbstractAnimation::State oldState).

If the startValue is not defined when the state of the animation changes from Stopped to Running, the current property value is used as the initial value for the animation.