Qt Animation Framework

    With the advent of Qt 4.6, the implementation of animation in the project has been simplified to a minimum.

    In the event that animation of component properties is needed, Qt 4.6 suggests using the QPropertyAnimation class . Here is an example widget resize animation:

    1. #include
    2. #include
    3. #include
    4.  
    5. class MyWidget : public QWidget {
    6. public:
    7.   MyWidget(QObject* parent = 0);
    8.   // MyWidget fields and methods
    9. public slots:
    10.   void startAnimation();
    11. private:
    12.   QPropertyAnimation* _propertyAnimation;
    13. };
    * This source code was highlighted with Source Code Highlighter.


    1. MyWidget::MyWidget(QObject *parent) : QWidget(parent) {
    2.   // Widget initialization
    3.   _propertyAnimation = new QPropertyAnimation(this,"geometry");
    4.   _propertyAnimation->setDuration(1000);
    5.   _propertyAnimation->setEasingCurve(QEasingCurve::OutCubic);
    6. }
    7.  
    8. void MyWidget::startAnimation() {
    9.   QRectF firstPosition;
    10.   QRectF endPosition;
    11.   // Initializing first and end values
    12.   _propertyAnimation->setFirstValue(firstPosition);
    13.   _propertyAnimation->setEndValue(endPosition);
    14.   _propertyAnimation->start();
    15. }
    * This source code was highlighted with Source Code Highlighter.


    Using the setDuration (int) method, the duration of the animation is set, and using the setEasingCurve (const QEasingCurve &) method, the curve of the value changes over time. In this example, the OutQuad curve is selected.


    Actually, 6 lines and all.

    To animate a non-property value, you can inherit from QVariantAnimation and override the void updateCurrentValue (const QVariant &) method . In this method, it is necessary to describe the logic of what should happen when the value changes.

    QVariantAnimation supports the following types: Int, Double, Float, QLine, QLineF, QPoint, QSize, QSizeF, QRect, QRectF. If the type of the value being changed does not belong to any of the above, you need to redefine the interpolation method virtual QVariant interpolated (const QVariant & from, const QVariant & to, qreal progress) const

    Here is an example of what happened:

    Also popular now: