Introduction to qt animation and qt Animation
The Qt-4.6 adds the Animation Framework, which allows us to easily write some vivid programs. Like in previous versions, all controls are boring and stay in the great and glorious QLayout. Maybe they can sing a song and dance.
The so-called animation has different States at different time points within a period of time. As long as the State is defined, it is a matter of course to implement the animation. Of course to do this thing, the best use is the state machine, right Qt-4.6.0 provides the QStateMachine class, but today I want to talk about the three words should be simple.
First, QPropertyAnimation
QPropertyAnimation is used to communicate with properties in QObject, such as QWidget size and coordinates. Look at the code
Reference 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 ();
The QPropertyAnimation object created in the first row is associated with the geometric properties of the form of myWidget. The following sentence sets the animation duration, starting coordinate, and ending cursor. You just need to change the QProperAnimation to do the rest. Call start () to start it. Yes, the five-line code completes a form that automatically moves from one coordinate point to another coordinate point. Below is a code that can be run. It is a small animation that a bird moves from the animation to the middle. Of course, you have to prepare the image with the same name by yourself :)
Copy code
The above example uses the position attribute pos of the label. Of course, you can add other properties in your class, such as changing the color.
Decision 2: setEasingCurve
In the above example, the movement of small birds is linear, which is not too monotonous. The void setEasingCurve (const QEasingCurve & easing) function in QPropertyAnimation is used to implement different curvature changes. The list of available parameters (including function curves) of QEasingCurve can be found in this document. Change the animation-related code section
Reference QPropertyAnimation * anim1 = new QPropertyAnimation (bird_1, "pos ");
Anim1-> setDuration (2000 );
Anim1-> setStartValue (QPoint (0,360 ));
Anim1-> setEndValue (QPoint (110,180 ));
Anim1-> setEasingCurve (QEasingCurve: OutBounce );
Anim1-> start ();
Note: The fourth sentence is added. Try other curve parameters and run them to see if the dynamic effects are different. If you are not satisfied with the existing curves in the list, you can inherit QEasingCurve to achieve the desired effect.
Conclusion 3: QAnimationGroup
In the previous example, only one animation is running. If you want to run multiple Animations together, you need to use the animation group QAnimationGroup. The animation groups are serial and parallel, which correspond to the QSequentialAnimationGroup and QParallelAnimationGroup sub-classes of QAnimationGroup. Its usage is simple.
Reference QSequentialAnimationGroup group;
// QParallelAnimationGroup;
Group. addAnimation (anim1 );
Group. addAnimation (anim2 );
Group. start ();
The above code, if it is serial, then anim2 will be run after the animation anim1 is run. In parallel, the two animations run simultaneously. If an animation group is added, you do not need to call a single anim1-> start (), which is managed by the animation group. The following is a runable code. The two birds move from the upper left corner and the lower right corner of the form to the middle.
Copy code