Creating and setting states
要创建状态,添加 State object to the item’s states 特性,其保持该项的状态列表。
警告
signalcomponent may have two states, theNORMAL和CRITICALstate. Suppose that in theNORMALstate, thecolorof the signal should begreenand the warningflagis down. Meanwhile, in theCRITICALstate, thecolor应该为redand the flag isup. We may model the states using theStatetype and the color and flag configurations with thePropertyChanges类型。Rectangle { id: signal width: 200; height: 200 state: "NORMAL" states: [ State { name: "NORMAL" PropertyChanges { target: signal; color: "green"} PropertyChanges { target: flag; state: "FLAG_DOWN"} }, State { name: "CRITICAL" PropertyChanges { target: signal; color: "red"} PropertyChanges { target: flag; state: "FLAG_UP"} } ] }PropertyChanges type will change the values of object properties. Objects are referenced through their id . Objects outside the component are also referenced using the
idproperty, exemplified by the property change to the externalflag对象。Further, the state may change by assigning the
stateproperty with the appropriate signal state. A state switch could be in a MouseArea type, assigning a different state whenever the signal receives a mouse click.Rectangle { id: signalswitch width: 75; height: 75 color: "blue" MouseArea { anchors.fill: parent onClicked: { if (signal.state == "NORMAL") signal.state = "CRITICAL" else signal.state = "NORMAL" } } }The State type is not limited to performing modifications on property values. It can also:
运行某些脚本使用 StateChangeScript
Override an existing signal handler for an object using PropertyChanges
重新父级 Item 使用 ParentChange
修改锚点值使用 AnchorChanges
Every Item based component has a
stateproperty 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 thestateproperty to an empty string will load the default state.
when
For convenience, the State 类型拥有
whenproperty that can bind to expressions to change the state whenever the bound expression evaluates totrue。whenproperty will revert the state back to the default state when the expression evaluates to false.Rectangle { id: bell width: 75; height: 75 color: "yellow" states: State { name: "RINGING" when: (signal.state == "CRITICAL") PropertyChanges {target: speaker; play: "RING!"} } }
bellcomponent will change to theRINGINGstate whenever thesignal.stateisCRITICAL.
State changes induce abrupt value changes. The Transition type allow smoother changes during state changes. In transitions, animations and interpolation behaviors are definable. The 动画和过渡 article has more information about creating state animations.
动画 example demonstrates how to declare a basic set of states and apply animated transitions between them.
Using Qt Quick Behaviors with States explains a common problem when using Behaviors to animate state changes.
In order for Transition to correctly animate state changes, it is sometimes necessary for the engine to fast forward and rewind a state (that is, internally set and unset the state) before it is finally applied. The process is as follows:
The state is fast forwarded to determine the complete set of end values.
The state is rewound.
The state is fully applied, with transitions.
In some cases this may cause unintended behavior. For example, a state that changes a view’s model or a Loader’s sourceComponent will set these properties multiple times (to apply, rewind, and then reapply), which can be relatively expensive.
State fast forwarding should be considered an implementation detail, and may change in later versions.