Qt animation: animation framework

1. Introduction to Qt animation class

class name

Features

QAbstractAnimation animation base class

Provides basic animation properties and interfaces, it has two subclasses QVariantAnimation and QAnimationGroup. QAbstractAnimation is the parent class of all other classes. It provides basic properties for all animations under this framework.

QPropertyAnimation the actual animation class

A Qt animation property is implemented, such as the animation effect of the control's size scaling, position movement, and transparency change. The properties to be modified must be properties of the class, and the class must have property definitions Q_PROPERTY (QRect geometry READ geometry WRITE setGeometry), otherwise the properties must be declared and the READ and WRITE methods implemented.

QParallelAnimationGroup parallel animation class

Add multiple property animations QPropertyAnimation to a QParallelAnimationGroup to implement animations in parallel.

QSequentialAnimationGroup serial animation class

QSequentialAnimationGroup implements multiple QPropertyAnimations in series, and executes animations in the order of addition.

QPauseAnimation pause class

In serial animation, adding a paused animation can achieve a delay effect.

QEasingCurve speed curve class

There are 45 types of velocity curves for Qt animation motion, see the appendix for details.

QPropertyAnimation is an animation class that comes with Qt. This class can implement simple control animation effects, such as animation effects on control movement, scaling, and opacity (you need to use the setPropertyName function to specify the desired animation property name before using an effect. The following three are already defined by Qt).

  • Move (pos): Mainly realize the effect of moving, such as moving from a certain point to another point, the variable type used is QPoint and so on.
  • Scaling (geometry): It can achieve zooming and moving effects. This property can realize the zooming of the specified control, and it can also realize movement on the basis of zooming.
  • Opacity (windowOpacity): realize the transparency setting of the control (however, this property can only be used for top-level windows, and it is invalid for ordinary controls).

The benefits of this article, free to receive Qt development learning materials package, technical video, including (C++ language foundation, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓

Two, QPropertyAnimation

The QPropertyAnimation class can modify the property values ​​of Qt, such as pos, geometry and other properties. After setting the initial value and end value of the animation, as well as the duration, a property animation is basically completed.

2.1 Mobile

If you only need displacement animation, you can modify the pos property of the control.

QPropertyAnimation *pAnimation = new QPropertyAnimation(ui->scaleButton, "pos");
pAnimation->setDuration(300); // 设置动画执行时间,单位毫秒
pAnimation->setStartValue(QPoint(140, 688)); // 初始值
pAnimation->setEndValue(QPoint(140, 668)); // 结束值

2.2 Zoom

By modifying the geometry property of the control, the zoom effect can be realized, and the animation of the displacement can also be realized. The first two values ​​of the property determine the position of the upper left corner of the control, and the last two values ​​determine the size of the control.

QPropertyAnimation *pAnimation = new QPropertyAnimation(ui->scaleButton, "geometry");
pAnimation->setDuration(300); // 设置动画执行时间,单位毫秒
pAnimation->setStartValue(QRect(140, 688, 534, 48)); // 初始值
pAnimation->setEndValue(QRect(140, 688, 534, 96)); // 结束值
pAnimation->setEasingCurve(QEasingCurve::InOutQuad); // 设置速度曲线
//pAnimation->start(QAbstractAnimation::DeleteWhenStopped); // 执行动画,结束后删除对象

2.3 Opacity

Because the Qt control does not have a transparency property opacity, it is necessary to implement the control drawing effect through QGraphicsOpacityEffect to achieve transparency change.

QGraphicsOpacityEffect *pButtonOpacity = new QGraphicsOpacityEffect(this);
pButtonOpacity->setOpacity(1);
ui->scaleButton->setGraphicsEffect(pButtonOpacity);

QPropertyAnimation *pOpacityAnimation1 = new QPropertyAnimation(pButtonOpacity, "opacity");
pOpacityAnimation1->setDuration(1000);
pOpacityAnimation1->setStartValue(1);
pOpacityAnimation1->setEndValue(0);

2.4 Animation Curve

The animation can also set the time interpolation curve, the default is linear, that is, linear motion, by setting QEasingCurve. Qt provides 40 defined curves (you can also customize the curve if necessary):

pAnimation->setEasingCurve(QEasingCurve::InOutQuad);

2.5 Animation Execution Direction Setting

Qt animation execution can set the direction of execution, which is Forward and Backward. For example, the transparency change is the initial value setStartValue(0); the end value is setEndValue(1); the control changes from hiding to displaying. You can set the animation execution direction to reverse, pAnimation–>setDirection(QAbstractAnimation::Backward), so that you can perform the animation from display to hiding. Animation execution is generally bi-directional and can be controlled by direction to avoid creating a reverse animation flow.

enum Direction {
   Forward,
   Backward
};

// 设置动画执行方向为反向
pAnimation–>setDirection(QAbstractAnimation::Backward);

2.6 Animation cycle times setting

Sometimes it is necessary to perform animation multiple times, or to loop infinitely, you can set the number of animation loops. When set to -1, it means infinite loop.

void setLoopCount(int loopCount);

2.7 Actions at the end of animation execution

To perform some cleaning work or property setting work after the animation ends, you can connect the QAbstractAnimation::finished signal, and perform some operations after the animation ends. It is also possible to put the cleanup of the animation object into the slot function. So you can't start(QAbstractAnimation::DeleteWhenStopped);

connect(pSequenAno, &QAbstractAnimation::finished, [=]() {
	ui->pushButtonAddVideo->setDisabled(true);
    ui->pushButtonDelVideo->setDisabled(true);
    ui->pushButtonAddPic->setDisabled(true);
    ui->pushButtonDelPic->setDisabled(true);
    PausePlayVedio(1, 0);//先创建索引,准确时间定位
});

3. QSequentialAnimationGroup

QSequentialAnimationGroup is a group of serial animations. By adding QPropertyAnimation or QPauseAnimation, an animation group is formed that is played in order of addition. The total duration of the animation group is the sum of the animations added.

QSequentialAnimationGroup *pPosGroup = new QSequentialAnimationGroup(this);
pPosGroup->addPause(500);
pPosGroup->addAnimation(pAnimation1);
pPosGroup->addAnimation(pAnimation2);

Back and forth

Qt's animation can set the number of cycles. The default cycle is to play it again from the beginning. The round-trip motion can be realized by adding a group of animations with the opposite initial value and final value in a serial animation group.

Four, QParallelAnimationGroup

QParallelAnimationGroup is a parallel animation group. The animations added to the parallel animation group will be played at the same time. The total duration of the animation group is the time required for the longest animation.

m_group = new QParallelAnimationGroup(this);
m_group->addAnimation(pScaleGroup);
m_group->addAnimation(pPosGroup);
m_group->addAnimation(pOpacityGroup);

delayed playback

Add a QPauseAnimation at the beginning of the serial animation group, and then add different serial animation groups of Pause to the parallel animation group to achieve the delay effect.

The benefits of this article, free to receive Qt development learning materials package, technical video, including (C++ language foundation, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓

Guess you like

Origin blog.csdn.net/QtCompany/article/details/131603592