QVariantAnimation Class Reference

The QVariantAnimation class provides an abstract base class for animations.

  1. #include <QVariantAnimation>

Inherits: QAbstractAnimation.

Inherited by: QPropertyAnimation.

This class was introduced in Qt 4.6.

Detailed Description

The QVariantAnimation class provides an abstract base class for animations.

This class is part of The Animation Framework. It serves as a base class for property and item animations, with functions for shared functionality.

QVariantAnimation cannot be used directly as it is an abstract class; it has a pure virtual method called updateCurrentValue(). The class performs interpolation over QVariants, but leaves using the interpolated values to its subclasses. Currently, Qt provides QPropertyAnimation, which animates Qt properties. See the QPropertyAnimation class description if you wish to animate such properties.

You can then set start and end values for the property by calling setStartValue() and setEndValue(), and finally call start() to start the animation. QVariantAnimation will interpolate the property of the target object and emit valueChanged(). To react to a change in the current value you have to reimplement the updateCurrentValue() virtual function.

It is also possible to set values at specified steps situated between the start and end value. The interpolation will then touch these points at the specified steps. Note that the start and end values are defined as the key values at 0.0 and 1.0.

There are two ways to affect how QVariantAnimation interpolates the values. You can set an easing curve by calling setEasingCurve(), and configure the duration by calling setDuration(). You can change how the QVariants are interpolated by creating a subclass of QVariantAnimation, and reimplementing the virtual interpolated() function.

Subclassing QVariantAnimation can be an alternative if you have QVariants that you do not wish to declare as Qt properties. Note, however, that you in most cases will be better off declaring your QVariant as a property.

Not all QVariant types are supported. Below is a list of currently supported QVariant types:

If you need to interpolate other variant types, including custom types, you have to implement interpolation for these yourself. To do this, you can register an interpolator function for a given type. This function takes 3 parameters: the start value, the end value and the current progress.

Example:

  1.                 QVariant myColorInterpolator(const QColor &start, const QColor &end, qreal progress)
  2.  {
  3.     ...
  4.     return QColor(...);
  5. }
  6. ...
  7. qRegisterAnimationInterpolator<QColor>(myColorInterpolator);

Another option is to reimplement interpolated(), which returns interpolation values for the value being interpolated.

See also QPropertyAnimation, QAbstractAnimation, and The Animation Framework.

Types

Toggle detailstypedef QVariantAnimation KeyValueKeyValue

This is a typedef for QPair<qreal, QVariant>.

Look up this member in the source code.

Toggle detailstypedef QVariantAnimation KeyValuesKeyValues

This is a typedef for QVector<KeyValue>

Look up this member in the source code.

    Properties

    Toggle detailscurrentValuecurrentValue : const QVariant

    This property holds the current value of the animation.

    This property describes the current value; an interpolated value between the start value and the end value, using the current time for progress. The value itself is obtained from interpolated(), which is called repeatedly as the animation is running.

    QVariantAnimation calls the virtual updateCurrentValue() function when the current value changes. This is particularly useful for subclasses that need to track updates. For example, QPropertyAnimation uses this function to animate Qt properties.

    See also startValue and endValue.

    Access functions:

    Look up this member in the source code.

    Toggle detailsdurationduration : int

    This property holds the duration of the animation

    This property describes the duration in milliseconds of the animation. The default duration is 250 milliseconds.

    See also QAbstractAnimation::duration().

    Access functions:

    Look up this member in the source code.

    Toggle detailseasingCurveeasingCurve : QEasingCurve

    This property holds the easing curve of the animation

    This property defines the easing curve of the animation. By default, a linear easing curve is used, resulting in linear interpolation. Other curves are provided, for instance, QEasingCurve::InCirc, which provides a circular entry curve. Another example is QEasingCurve::InOutElastic, which provides an elastic effect on the values of the interpolated variant.

    QVariantAnimation will use the QEasingCurve::valueForProgress() to transform the "normalized progress" (currentTime / totalDuration) of the animation into the effective progress actually used by the animation. It is this effective progress that will be the progress when interpolated() is called. Also, the steps in the keyValues are referring to this effective progress.

    The easing curve is used with the interpolator, the interpolated() virtual function, the animation's duration, and iterationCount, to control how the current value changes as the animation progresses.

    Access functions:

    Look up this member in the source code.

    Toggle detailsendValueendValue : QVariant

    This property holds the end value of the animation

    This property describes the end value of the animation.

    See also startValue.

    Access functions:

    Look up this member in the source code.

    Toggle detailsstartValuestartValue : QVariant

    This property holds the optional start value of the animation

    This property describes the optional start value of the animation. If omitted, or if a null QVariant is assigned as the start value, the animation will use the current position of the end when the animation is started.

    See also endValue.

    Access functions:

    Look up this member in the source code.

      Public Functions

      Toggle details QVariantAnimation

      QVariantAnimationQVariantAnimation ( QObject *parent=0 ) ( QObject *parent=0 )

      Construct a QVariantAnimation object. parent is passed to QAbstractAnimation's constructor.

      Look up this member in the source code.

      Toggle details QVariantAnimation

      ~QVariantAnimation~QVariantAnimation () ()

      Destroys the animation.

      Look up this member in the source code.

      Toggle details bool QVariantAnimation

      eventevent ( QEvent *event ) ( QEvent *event ) [virtual protected]

      Reimplemented from QObject::event().

      Look up this member in the source code.

      Toggle details QVariant QVariantAnimation

      interpolatedinterpolated ( const QVariant &from , const QVariant &to , qreal progress ...) ( const QVariant &from , const QVariant &to , qreal progress )const [virtual protected]

      This virtual function returns the linear interpolation between variants from and to, at progress, usually a value between 0 and 1. You can reimplement this function in a subclass of QVariantAnimation to provide your own interpolation algorithm.

      Note that in order for the interpolation to work with a QEasingCurve that return a value smaller than 0 or larger than 1 (such as QEasingCurve::InBack) you should make sure that it can extrapolate. If the semantic of the datatype does not allow extrapolation this function should handle that gracefully.

      You should call the QVariantAnimation implementation of this function if you want your class to handle the types already supported by Qt (see class QVariantAnimation description for a list of supported types).

      See also QEasingCurve.

      Look up this member in the source code.

      Toggle details QVariant QVariantAnimation

      keyValueAtkeyValueAt ( qreal step ) ( qreal step )const

      Returns the key frame value for the given step. The given step must be in the range 0 to 1. If there is no KeyValue for step, it returns an invalid QVariant.

      See also keyValues() and setKeyValueAt().

      Look up this member in the source code.

      Toggle details KeyValues QVariantAnimation

      keyValueskeyValues () ()const

      Returns the key frames of this animation.

      See also keyValueAt() and setKeyValues().

      Look up this member in the source code.

      Toggle details void QVariantAnimation

      setKeyValueAtsetKeyValueAt ( qreal step , const QVariant &value ...) ( qreal step , const QVariant &value )

      Creates a key frame at the given step with the given value. The given step must be in the range 0 to 1.

      See also setKeyValues() and keyValueAt().

      Look up this member in the source code.

      Toggle details void QVariantAnimation

      setKeyValuessetKeyValues ( const KeyValues &keyValues ) ( const KeyValues &keyValues )

      Replaces the current set of key frames with the given keyValues. the step of the key frames must be in the range 0 to 1.

      See also keyValues() and keyValueAt().

      Look up this member in the source code.

      Toggle details void QVariantAnimation

      updateCurrentTimeupdateCurrentTime ( int ) ( int ) [virtual protected]

      Look up this member in the source code.

      Toggle details void QVariantAnimation

      updateCurrentValueupdateCurrentValue ( const QVariant &value ) ( const QVariant &value ) [pure virtual protected]

      This pure virtual function is called every time the animation's current value changes. The value argument is the new current value.

      See also currentValue.

      Look up this member in the source code.

      Toggle details void QVariantAnimation

      updateStateupdateState ( QAbstractAnimation::State newState , QAbstractAnimation::State oldState ...) ( QAbstractAnimation::State newState , QAbstractAnimation::State oldState ) [virtual protected]

      Reimplemented from QAbstractAnimation::updateState().

      Look up this member in the source code.

        Signals

        Toggle details void QVariantAnimation

        valueChangedvalueChanged ( const QVariant &value ) ( const QVariant &value ) [signal]

        QVariantAnimation emits this signal whenever the current value changes.

        See also currentValue, startValue, and endValue.

        Look up this member in the source code.

          Notes provided by the Qt Community
          Cool Hack
          • 5

          Votes: 2

          Coverage: Qt library 4.7, 4.8

          Picture of Andre Andre

          Robot Herder
          30 notes

          Nokia Certified Qt Developer

          QVariantAnimation is unfortunately abstract

          QVariantAnimation is unfortunately abstract, because there is one pure virtual method: updateCurrentValue(). You don’t really need an implementation of that method to use QVariantAnimation in a useful way though, so this can be considdered a bug [bugreports.qt.nokia.com] in Qt.

          To work around this issue, I suggest you create a class defined like this:

          1. class VariantAnimation : public QVariantAnimation
          2. {
          3.   Q_OBJECT
          4. public:
          5.   VariantAnimation(QObject* parent = 0):
          6.   QVariantAnimation(parent) {}
          7.  
          8. protected:
          9.   virtual void updateCurrentValue(const QVariant& value) { Q_UNUSED(value); }
          10. };

          Using this class instead of QVariantAnimation, you can just rely on the valueChanged() signal to do your animation.

          Edit: This has been fixed [codereview.qt-project.org] for Qt 5.0, where QVariantAnimation is no longer abstract.

          [Revisions]