QML States

States Elements

Many user interface designs are state driven; interfaces have configurations that differ depending on the current state. For example, a traffic signal will configure its flags or lights depending on its state. While in the signal's stop state, a red light will turn on while the yellow and the green lights will turn off. In the caution state, the yellow light is on while the other lights are turned off.

In QML, states are a set of property configurations defined in a State element. Different configurations could, for example:

  • Show some UI elements and hide others
  • Present different available actions to the user
  • Start, stop, or pause animations
  • Execute some script required in the new state
  • Change a property value for a particular item
  • Show a different view or screen

All Item-based objects have a state property, and can specify additional states by adding new State objects to the item's states property. Each state within a component has a unique name, an empty string being the default. To change the current state of an item, set the state property to the name of the state.

Non-Item objects may use states through the StateGroup element.

Creating States

To create a state, add a State object to the item's states property, which holds a list of states for that item.

A warning signal component may have two states, the NORMAL and the CRITICAL state. Suppose that in the NORMAL state, the color of the signal should be green and the warning flag is down. Meanwhile, in the CRITICAL state, the color should be red and the flag is up. We may model the states using the State element and the color and flag configurations with the PropertyChanges element.

  1.                 Rectangle  {
  2.     id: signal
  3.     width: 200; height: 200
  4.     state: "NORMAL"
  5.  
  6.     states: [
  7.         State  {
  8.             name: "NORMAL"
  9.             PropertyChanges  { target: signal; color: "green"}
  10.             PropertyChanges  { target: flag; state: "FLAG_DOWN"}
  11.         },
  12.         State  {
  13.             name: "CRITICAL"
  14.             PropertyChanges  { target: signal; color: "red"}
  15.             PropertyChanges  { target: flag; state: "FLAG_UP"}
  16.         }
  17.     ]
  18. }

The PropertyChanges element will change the values of object properties. Objects are referenced through their id. Objects outside the component are also referenced using the id property, exemplified by the property change to the external flag object.

Further, the state may change by assigning the state property with the appropriate signal state. A state switch could be in a MouseArea element, assigning a different state whenever the signal receives a mouse click.

  1.                 Rectangle  {
  2.     id: signalswitch
  3.     width: 75; height: 75
  4.     color: "blue"
  5.  
  6.     MouseArea  {
  7.         anchors.fill: parent
  8.         onClicked:  {
  9.             if (signal.state == "NORMAL")
  10.                 signal.state = "CRITICAL"
  11.             else
  12.                 signal.state = "NORMAL"
  13.         }
  14.     }
  15. }

The State element is not limited to performing modifications on property values. It can also:

The Default State

Every Item based component has a state property and a default state. The default state is the empty string ("") and contains all of an item's initial property values. The default state is useful for managing property values before state changes. Setting the state property to an empty string will load the default state.

The when Property

For convenience, the State element has a when property that can bind to expressions to change the state whenever the bound expression evaluates to true. The when property will revert the state back to the default state when the expression evaluates to false.

  1.                 Rectangle  {
  2.     id: bell
  3.     width: 75; height: 75
  4.     color: "yellow"
  5.  
  6.     states: State  {
  7.                 name: "RINGING"
  8.                 when: (signal.state == "CRITICAL")
  9.                 PropertyChanges  {target: speaker; play: "RING!"}
  10.             }
  11. }

The bell component will change to the RINGING state whenever the signal.state is CRITICAL.

Animating State Changes

State changes induce abrupt value changes. The Transition element allow smoother changes during state changes. In transitions, animations and interpolation behaviors are definable. The Animation and Transitions article has more information about creating state animations.

The States and Transitions example demonstrates how to declare a basic set of states and apply animated transitions between them.

Notes provided by the Qt Community

No notes