Qt 4.8
State Class Reference

The State element defines configurations of objects and properties. More...

Detailed Description

The State element defines configurations of objects and properties.

Since
4.7

A state is a set of batched changes from the default configuration.

All items have a default state that defines the default configuration of objects and property values. New states can be defined by adding State items to the Item::states{states} property to allow items to switch between different configurations. These configurations can, for example, be used to apply different sets of property values or execute different scripts.

The following example displays a single Rectangle . In the default state, the rectangle is colored black. In the "clicked" state, a PropertyChanges element changes the rectangle's color to red. Clicking within the MouseArea toggles the rectangle's state between the default state and the "clicked" state, thus toggling the color of the rectangle between black and red.

import QtQuick 1.0
Rectangle {
id: myRect
width: 100; height: 100
color: "black"
MouseArea {
id: mouseArea
anchors.fill: parent
onClicked: myRect.state == 'clicked' ? myRect.state = "" : myRect.state = 'clicked';
}
states: [
State {
name: "clicked"
PropertyChanges { target: myRect; color: "red" }
}
]
}

Notice the default state is referred to using an empty string ("").

States are commonly used together with QML Animation and Transitions{Transitions} to provide animations when state changes occur.

Note
Setting the state of an object from within another state of the same object is not allowed.
See also
{declarative/animation/states}{states example}, {qmlstates}{States}, {QML Animation and Transitions}{Transitions}, QtDeclarative

The documentation for this class was generated from the following file: