📜 ⬆️ ⬇️

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 you need to animate component properties, Qt 4.6 offers to use the QPropertyAnimation class. Here is an example of a widget's resize animation:

#include <QWidget> #include <QPropertyAnimation> #include <QEasingCurve> class MyWidget : public QWidget { public : MyWidget(QObject* parent = 0); // MyWidget fields and methods public slots: void startAnimation(); private : QPropertyAnimation* _propertyAnimation; }; * This source code was highlighted with Source Code Highlighter .
  1. #include <QWidget> #include <QPropertyAnimation> #include <QEasingCurve> class MyWidget : public QWidget { public : MyWidget(QObject* parent = 0); // MyWidget fields and methods public slots: void startAnimation(); private : QPropertyAnimation* _propertyAnimation; }; * This source code was highlighted with Source Code Highlighter .
  2. #include <QWidget> #include <QPropertyAnimation> #include <QEasingCurve> class MyWidget : public QWidget { public : MyWidget(QObject* parent = 0); // MyWidget fields and methods public slots: void startAnimation(); private : QPropertyAnimation* _propertyAnimation; }; * This source code was highlighted with Source Code Highlighter .
  3. #include <QWidget> #include <QPropertyAnimation> #include <QEasingCurve> class MyWidget : public QWidget { public : MyWidget(QObject* parent = 0); // MyWidget fields and methods public slots: void startAnimation(); private : QPropertyAnimation* _propertyAnimation; }; * This source code was highlighted with Source Code Highlighter .
  4. #include <QWidget> #include <QPropertyAnimation> #include <QEasingCurve> class MyWidget : public QWidget { public : MyWidget(QObject* parent = 0); // MyWidget fields and methods public slots: void startAnimation(); private : QPropertyAnimation* _propertyAnimation; }; * This source code was highlighted with Source Code Highlighter .
  5. #include <QWidget> #include <QPropertyAnimation> #include <QEasingCurve> class MyWidget : public QWidget { public : MyWidget(QObject* parent = 0); // MyWidget fields and methods public slots: void startAnimation(); private : QPropertyAnimation* _propertyAnimation; }; * This source code was highlighted with Source Code Highlighter .
  6. #include <QWidget> #include <QPropertyAnimation> #include <QEasingCurve> class MyWidget : public QWidget { public : MyWidget(QObject* parent = 0); // MyWidget fields and methods public slots: void startAnimation(); private : QPropertyAnimation* _propertyAnimation; }; * This source code was highlighted with Source Code Highlighter .
  7. #include <QWidget> #include <QPropertyAnimation> #include <QEasingCurve> class MyWidget : public QWidget { public : MyWidget(QObject* parent = 0); // MyWidget fields and methods public slots: void startAnimation(); private : QPropertyAnimation* _propertyAnimation; }; * This source code was highlighted with Source Code Highlighter .
  8. #include <QWidget> #include <QPropertyAnimation> #include <QEasingCurve> class MyWidget : public QWidget { public : MyWidget(QObject* parent = 0); // MyWidget fields and methods public slots: void startAnimation(); private : QPropertyAnimation* _propertyAnimation; }; * This source code was highlighted with Source Code Highlighter .
  9. #include <QWidget> #include <QPropertyAnimation> #include <QEasingCurve> class MyWidget : public QWidget { public : MyWidget(QObject* parent = 0); // MyWidget fields and methods public slots: void startAnimation(); private : QPropertyAnimation* _propertyAnimation; }; * This source code was highlighted with Source Code Highlighter .
  10. #include <QWidget> #include <QPropertyAnimation> #include <QEasingCurve> class MyWidget : public QWidget { public : MyWidget(QObject* parent = 0); // MyWidget fields and methods public slots: void startAnimation(); private : QPropertyAnimation* _propertyAnimation; }; * This source code was highlighted with Source Code Highlighter .
  11. #include <QWidget> #include <QPropertyAnimation> #include <QEasingCurve> class MyWidget : public QWidget { public : MyWidget(QObject* parent = 0); // MyWidget fields and methods public slots: void startAnimation(); private : QPropertyAnimation* _propertyAnimation; }; * This source code was highlighted with Source Code Highlighter .
  12. #include <QWidget> #include <QPropertyAnimation> #include <QEasingCurve> class MyWidget : public QWidget { public : MyWidget(QObject* parent = 0); // MyWidget fields and methods public slots: void startAnimation(); private : QPropertyAnimation* _propertyAnimation; }; * This source code was highlighted with Source Code Highlighter .
#include <QWidget> #include <QPropertyAnimation> #include <QEasingCurve> class MyWidget : public QWidget { public : MyWidget(QObject* parent = 0); // MyWidget fields and methods public slots: void startAnimation(); private : QPropertyAnimation* _propertyAnimation; }; * 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. void MyWidget :: startAnimation () {
  8. QRectF firstPosition;
  9. QRectF endPosition;
  10. // Initializing first and end values
  11. _propertyAnimation-> setFirstValue (firstPosition);
  12. _propertyAnimation-> setEndValue (endPosition);
  13. _propertyAnimation-> start ();
  14. }
* This source code was highlighted with Source Code Highlighter .

')
The setDuration (int) method sets the duration of the animation, and the setEasingCurve method (const QEasingCurve &) sets the value change curve over time. In this example, the OutQuad curve is selected.


Actually, 6 lines and all.

To animate a value that is not a property, you can inherit 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 changeable value does not belong to any of the above, you need to override the interpolation method. Virtual QVariant interpolated (const QVariant & from, const QVariant & to, qreal progress) const

Here is an example of what happened:

Source: https://habr.com/ru/post/75671/


All Articles