User interfaces are designed to present different interface configurations in different scenarios, or to modify their appearances in response to user interaction. Often, there are a set of changes that are made concurrently, such that the interface could be seen to be internally changing from one state to another.
This applies generally to interfaces regardless of their complexity. A photo viewer may initially present images in a grid, and when an image is clicked, change to a detailed state where the individual image is expanded and the interface is changed to present new options for image editing. At the other end of the scale, when a button is pressed, it may change to a pressed state in which its color and position are modified so that it appears to be pressed down.
In QML, any item can change between different states to apply sets of changes that modify the properties of relevant items. Each state can present a different configuration that can, for example:
- Show some UI items 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.
The States view displays the different states of a UI, beginning with a base state.
To add states, click the button. Click the new state to switch to it in Form Editor, and then modify the properties of components in Properties.
For example, to change the appearance of a button, you can hide the button image and show another image in its place. Or, to add movement to the view, you can change the position of an object in Form Editor and then add animation to the change between the states.
The properties that you change in a state are highlighted with blue color. In Text Editor, you can see the changes recorded as changes to the base state.
To determine the startup state of the application, select to open the Actions menu, and then select Set as Default.
To reset the state later, select Actions > Reset Default.
To determine when a state should be applied, select Actions > Set when Condition. In Binding Editor, specify a when property for the state. Set the value of the property to an expression that evaluates to
true when you want the state to be applied.
In Binding Editor, select the component and property to create the expression. For example, to change the state when a button is pressed, you could select a button component and its pressed property.
To keep the QML code clean, you should create a base state that contains all the types you will need in the application. You can then create states, in which you hide and show a set of items and modify their properties. This allows you to:
- Align items on different views with each other.
- Avoid excessive property changes. If an item is invisible in the base state, you must define all changes to its child types as property changes, which leads to complicated QML code.
- Minimize the differences between the base state and the other states to keep the QML code short and readable and to improve performance.
- Avoid problems when using transitions and animation when changing states.
To create views for an application by using states:
- In the base state, add all items you will need in the application (1). While you work on one view, you can click the icon to hide items on the canvas that are not part of a view.
- In States, click the empty slot to create a new state and give it a name. For example,
- In Properties (2), deselect the Visibility check box or set Opacity to 0 for each item that is not needed in this view. If you specify the setting for the parent item, all child items inherit it and are also hidden.
- Create additional states for each view and set the visibility or opacity of the items in the view.
- To determine which state is applied when the application starts, select Actions > Set as Default.
To make movement between states smooth, you can animate transitions. Animations are created by applying animation types to property values. Animation types 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 type for the type of the property that is to be animated, and apply the animation depending on the type of behavior that is required.
You can drag and drop the following QML types from Library > QML Types > Qt Quick - Animation to Navigator or Form Editor:
- Color Animation is a specialized property animation that defines an animation to be applied when a color value changes.
- Number Animation is a specialized property animation that defines an animation to be applied when a numerical value changes.
- Parallel Animation enables animations to be run in parallel.
- Pause Animation is used in a sequential animation to create a step where nothing happens, for a specified duration.
- Property Action immediately changes a property value during an animation, without animating the property change.
- Property Animation animates changes in the value of a property.
- Script Action defines scripts to be run during an animation.
- Sequential Animation enables animations to be run sequentially.
For more information, see Animation and Transitions in Qt Quick.
Alternatively, you can use the Timeline view to animate the properties of UI components and to bind the animations to states. For more information, see Binding Animations to States.
Available under certain Qt licenses.
Find out more.