QML Transition Element

The Transition element defines animated transitions that occur on state changes. More...

This element was introduced in Qt 4.7.

Properties

Detailed Description

A Transition defines the animations to be applied when a State change occurs.

For example, the following Rectangle has two states: the default state, and an added "moved" state. In the "moved state, the rectangle's position changes to (50, 50). The added Transition specifies that when the rectangle changes between the default and the "moved" state, any changes to the x and y properties should be animated, using an Easing.InOutQuad.

 import QtQuick 1.0

 Rectangle {
     id: rect
     width: 100; height: 100
     color: "red"

     MouseArea {
         id: mouseArea
         anchors.fill: parent
     }

     states: State {
         name: "moved"; when: mouseArea.pressed
         PropertyChanges { target: rect; x: 50; y: 50 }
     }

     transitions: Transition {
         NumberAnimation { properties: "x,y"; easing.type: Easing.InOutQuad }
     }
 }

Notice the example does not require to and from values for the NumberAnimation. As a convenience, these properties are automatically set to the values of x and y before and after the state change; the from values are provided by the current values of x and y, and the to values are provided by the PropertyChanges object. If you wish, you can provide to and from values anyway to override the default values.

By default, a Transition's animations are applied for any state change in the parent item. The Transition from and to values can be set to restrict the animations to only be applied when changing from one particular state to another.

To define multiple transitions, specify Item::transitions as a list:

 transitions: [
   Transition {
       from: "stop"; to: "go"
       PropertyAnimation { target: stopLight
                           properties: "color"; duration: 1000 }
   },
   Transition {
       from: "go"; to: "stop"
       PropertyAnimation { target: goLight
                           properties: "color"; duration: 1000 }
   } ]

If multiple Transitions are specified, only a single (best-matching) Transition will be applied for any particular state change. In the example above, when changing to state1, the first transition will be used, rather than the more generic second transition.

If a state change has a Transition that matches the same property as a Behavior, the Transition animation overrides the Behavior for that state change.

See also QML Animation and Transitions, states example, States, and QtDeclarative.

Property Documentation

read-onlydefaultanimations : list<Animation>

This property holds a list of the animations to be run for this transition.


The top-level animations are run in parallel. To run them sequentially, define them within a SequentialAnimation:

 transitions: Transition {
     SequentialAnimation {
         PropertyAnimation { property: "x"; duration: 1000 }
         ColorAnimation { duration: 1000 }
     }
 }

from : string

to : string

These properties indicate the state changes that trigger the transition.

The default values for these properties is "*" (that is, any state).

For example, the following transition has not set the to and from properties, so the animation is always applied when changing between the two states (i.e. when the mouse is pressed and released).

 Rectangle {
     id: rect
     width: 100; height: 100
     color: "red"

     MouseArea { id: mouseArea; anchors.fill: parent }

     states: State {
         name: "brighter"; when: mouseArea.pressed
         PropertyChanges { target: rect; color: "yellow" }
     }

     transitions: Transition {
         ColorAnimation { duration: 1000 }
     }
 }

If the transition was changed to this:

 transitions: Transition {
     to: "brighter"
     ColorAnimation { duration: 1000 }
 }

The animation would only be applied when changing from the default state to the "brighter" state (i.e. when the mouse is pressed, but not on release).

See also reversible.


reversible : bool

This property holds whether the transition should be automatically reversed when the conditions that triggered this transition are reversed.

The default value is false.

By default, transitions run in parallel and are applied to all state changes if the from and to states have not been set. In this situation, the transition is automatically applied when a state change is reversed, and it is not necessary to set this property to reverse the transition.

However, if a SequentialAnimation is used, or if the from or to properties have been set, this property will need to be set to reverse a transition when a state change is reverted. For example, the following transition applies a sequential animation when the mouse is pressed, and reverses the sequence of the animation when the mouse is released:

 Rectangle {
     id: rect
     width: 100; height: 100
     color: "red"

     MouseArea { id: mouseArea; anchors.fill: parent }

     states: State {
         name: "brighter"
         when: mouseArea.pressed
         PropertyChanges { target: rect; color: "yellow"; x: 50 }
     }

     transitions: Transition {
         SequentialAnimation {
             PropertyAnimation { property: "x"; duration: 1000 }
             ColorAnimation { duration: 1000 }
         }
     }
 }

If the transition did not set the to and reversible values, then on the mouse release, the transition would play the PropertyAnimation before the ColorAnimation instead of reversing the sequence.


Notes provided by the Qt Community

No notes