QML Animation and Transitions

Animation and Transitions Elements

Elements that animate properties based on data types

In QML, animations are created by applying animation elements to property values. Animation elements will interpolate property values to create smooth transitions. As well, state transitions may assign animations to state changes.

To create an animation, use an appropriate animation element for the type of the property that is to be animated, and apply the animation depending on the type of behavior that is required.

Triggering Animations

There are several ways of setting animation to an object.

Direct Property Animation

To create an immediate movement or animated movement, set the property value directly. This may be done in signal handlers or attached properties.

  1.                 Rectangle  {
  2.     id: blob
  3.     width: 75; height: 75
  4.     color: "blue"
  6.     MouseArea  {
  7.         anchors.fill: parent
  8.         onClicked: blob.color = "green"
  9.     }
  10. }

However, to create more control, property animations apply smooth movements by interpolating values between property value changes. Property animations provide timing controls and allows different interpolations through easing curves.

  1.                 Rectangle  {
  2.     id: flashingblob
  3.     width: 75; height: 75
  4.     color: "blue"
  5.     opacity: 1.0
  7.     MouseArea  {
  8.         anchors.fill: parent
  9.         onClicked:  {
  10.             animateColor.start()
  11.             animateOpacity.start()
  12.         }
  13.     }
  15.     PropertyAnimation  {id: animateColor; target: flashingblob; properties: "color"; to: "green"; duration: 100}
  17.     NumberAnimation  {
  18.         id: animateOpacity
  19.         target: flashingblob
  20.         properties: "opacity"
  21.         from: 0.99
  22.         to: 1.0
  23.         loops: Animation.Infinite
  24.         easing  {type: Easing.OutBack; overshoot: 500}
  25.    }
  26. }

Specialized property animation elements have more efficient implementations than the PropertyAnimation element. They are for setting animations to different QML types such as int, color, and rotations. Similarly, the ParentAnimation can animate parent changes.

See the Controlling Animations section for more information about the different animation properties.

Transitions during State Changes

States are property configurations where a property may have different values to reflect different states. State changes introduce abrupt property changes; animations smooth transitions to produce visually appealing state changes.

The Transition element can contain animation elements to interpolate property changes caused by state changes. To assign the transition to an object, bind it to the transitions property.

A button might have two states, the pressed state when the user clicks on the button and a released state when the user releases the button. We can assign different property configurations for each state. A transition would animate the change from the pressed state to the released state. Likewise, there would be an animation during the change from the released state to the pressed state.

  1.                 Rectangle  {
  2.     width: 75; height: 75
  3.     id: button
  4.     state: "RELEASED"
  6.     MouseArea  {
  7.         anchors.fill: parent
  8.         onPressed: button.state = "PRESSED"
  9.         onReleased: button.state = "RELEASED"
  10.     }
  12.     states: [
  13.         State  {
  14.             name: "PRESSED"
  15.             PropertyChanges  { target: button; color: "lightblue"}
  16.         },
  17.         State  {
  18.             name: "RELEASED"
  19.             PropertyChanges  { target: button; color: "lightsteelblue"}
  20.         }
  21.     ]
  23.     transitions: [
  24.         Transition  {
  25.             from: "PRESSED"
  26.             to: "RELEASED"
  27.             ColorAnimation  { target: button; duration: 100}
  28.         },
  29.         Transition  {
  30.             from: "RELEASED"
  31.             to: "PRESSED"
  32.             ColorAnimation  { target: button; duration: 100}
  33.         }
  34.     ]
  35. }

Binding the to and from properties to the state's name will assign that particular transition to the state change. For simple or symmetric transitions, setting the to to property to the wild card symbol, "*", denotes that the transition applies to any state change.

  1.     transitions:
  2.         Transition  {
  3.             to: "*"
  4.             ColorAnimation  { target: button; duration: 100}
  5.         }

Default Animation as Behaviors

Default property animations are set using behavior animations. Animations declared in Behavior elements apply to the property and animates any property value changes. However, Behavior elements have an enabled property to purposely enable or disable the behavior animations.

A ball component might have a behavior animation assigned to its x, y, and color properties. The behavior animation could be set up to simulate an elastic effect. In effect, this behavior animation would apply the elastic effect to the properties whenever the ball moves.

  1.                 Rectangle  {
  2.     width: 75; height: 75; radius: width
  3.     id: ball
  4.     color: "salmon"
  6.     Behavior on x  {
  7.         NumberAnimation  {
  8.             id: bouncebehavior
  9.             easing  {
  10.                 type: Easing.OutElastic
  11.                 amplitude: 1.0
  12.                 period: 0.5
  13.             }
  14.         }
  15.     }
  16.     Behavior on y  {
  17.         animation: bouncebehavior
  18.     }
  19.     Behavior  {
  20.         ColorAnimation  { target: ball; duration: 100 }
  21.     }
  22. }

There are several methods of assigning behavior animations to properties. The Behavior on <property> declaration is a convenient way of assigning a behavior animation onto a property.

See the Behaviors example for a demonstration of behavioral animations.

Playing Animations in Parallel or in Sequence

Animations can run in parallel or in sequence. Parallel animations will play a group of animations at the same time while sequential animations play a group of animations in order: one after the other. Grouping animations in SequentialAnimation and ParallelAnimation will play the animations in sequence or in parallel.

A banner component may have several icons or slogans to display, one after the other. The opacity property could transform to 1.0 denoting an opaque object. Using the SequentialAnimation element, the opacity animations will play after the preceding animation finishes. The ParallelAnimation element will play the animations at the same time.

  1.                 Rectangle  {
  2.     id: banner
  3.     width: 150; height: 100; border.color: "black"
  5.     Column  {
  6.         anchors.centerIn: parent
  7.         Text  {
  8.             id: code
  9.             text: "Code less."
  10.             opacity: 0.01
  11.         }
  12.         Text  {
  13.             id: create
  14.             text: "Create more."
  15.             opacity: 0.01
  16.         }
  17.         Text  {
  18.             id: deploy
  19.             text: "Deploy everywhere."
  20.             opacity: 0.01
  21.         }
  22.     }
  24.     MouseArea  {
  25.         anchors.fill: parent
  26.         onPressed: playbanner.start()
  27.     }
  29.     SequentialAnimation  {
  30.         id: playbanner
  31.         running: false
  32.         NumberAnimation  { target: code; property: "opacity"; to: 1.0; duration: 200}
  33.         NumberAnimation  { target: create; property: "opacity"; to: 1.0; duration: 200}
  34.         NumberAnimation  { target: deploy; property: "opacity"; to: 1.0; duration: 200}
  35.     }
  36. }

Once individual animations are placed into a SequentialAnimation or ParallelAnimation, they can no longer be started and stopped independently. The sequential or parallel animation must be started and stopped as a group.

The SequentialAnimation element is also useful for playing transition animations because animations are played in parallel inside transitions.

See the Animation basics example for a demonstration of creating and combining multiple animations in QML.

Controlling Animations

There are different methods to control animations.

Animation Playback

All animation elements inherit from the Animation element. It is not possible to create Animation objects; instead, this element provides the essential properties and methods for animation elements. Animation elements have start(), stop(), resume(), pause(), restart(), and complete() -- all of these methods control the execution of animations.


Easing curves define how the animation will interpolate between the start value and the end value. Different easing curves might go beyond the defined range of interpolation. The easing curves simplify the creation of animation effects such as bounce effects, acceleration, deceleration, and cyclical animations.

A QML object may have different easing curve for each property animation. There are also different parameters to control the curve, some of which are exclusive to a particular curve. For more information about the easing curves, visit the easing documentation.

The easing example visually demonstrates each of the different easing types.

Other Animation Elements

In addition, QML provides several other elements useful for animation:

  • PauseAnimation: enables pauses during animations
  • ScriptAction: allows JavaScript to be executed during an animation, and can be used together with StateChangeScript to reused existing scripts
  • PropertyAction: changes a property immediately during an animation, without animating the property change

These are specialized animation elements that animate different property types

Notes provided by the Qt Community

No notes