Note: Qt Quick Designer only supports Qt Quick 1, which is offered in Qt 5 only for compatibility with Qt 4. We recommend that you use Qt Quick 2 for new Qt 5 applications. You can use the code editor in the Design mode to develop Qt Quick 2 applications. For more information, see the Qt 5 documentation.
This tutorial uses built-in QML elements and illustrates basic concepts of Qt Quick 1.
This tutorial describes how to use the Qt Creator to implement the states and transitions example application. The example application displays a Qt logo that moves between three rectangles on the page when you click them.
For more information about using Qt Quick Designer, see Developing Qt Quick Applications.
Note: Kits are listed if they have been specified in Tools > Options > Build & Run > Kits.
Qt Creator generates a default QML file that you can modify to create the main view of the application.
The main view of the application displays a Qt logo in the top left corner of the screen and two empty rectangles.
To use the states.png image in your application, you must copy it to the project directory (same subdirectory as the QML file) from the examples directory in the Qt installation directory. For example: C:\QtSDK\Examples\4.7\declarative\animation\states. The image appears in the Resources pane. You can also use any other image or a QML element, instead.
Note: If the Border field does not appear after you set the border color, try setting the border color to solid by clicking the (Solid Color) button.
MouseArea { anchors.fill: parent onClicked: page.state = '' }
The expression sets the state to the base state and returns the image to its initial position.
The qml.main file should now look as follows:
import QtQuick 1.1 Rectangle { id: page width: 360 height: 360 color: "#343434" Image { id: icon x: 10 y: 20 source: "states.png" } Rectangle { id: topLeftRect y: 20 width: 64 height: 64 color: "#00000000" radius: 6 anchors.left: parent.left anchors.leftMargin: 10 anchors.top: parent.top anchors.topMargin: 20 border.color: "#808080" MouseArea { id: mousearea1 anchors.fill: parent onClicked: page.state = ' ' } }
onClicked: page.state = 'State1'
You will create State1 later.
onClicked: page.state = 'State2'
You will create State2 later.
The qml.main file should now look as follows:
import QtQuick 1.1 Rectangle { id: page width: 360 height: 360 color: "#343434" Image { id: icon x: 10 y: 20 source: "states.png" } Rectangle { id: topLeftRect y: 20 width: 64 height: 64 color: "#00000000" radius: 6 anchors.left: parent.left anchors.leftMargin: 10 anchors.top: parent.top anchors.topMargin: 20 border.color: "#808080" MouseArea { id: mousearea1 anchors.fill: parent onClicked: page.state = ' ' } } Rectangle { id: middleRightRect x: 13 y: 16 width: 64 height: 64 color: "#00000000" radius: 6 anchors.right: parent.right anchors.rightMargin: 10 anchors.verticalCenter: parent.verticalCenter border.color: "#808080" MouseArea { id: mousearea2 anchors.fill: parent onClicked: page.state = 'State1' } } Rectangle { id: bottomLeftRect y: 11 width: 64 height: 64 color: "#00000000" radius: 6 anchors.left: parent.left anchors.leftMargin: 10 anchors.bottom: parent.bottom anchors.bottomMargin: 20 border.color: "#808080" MouseArea { id: mousearea3 anchors.fill: parent onClicked: page.state = 'State2' } }
You should see the Qt logo in the top left rectangle, and two additional rectangles in the center right and bottom left of the screen.
You can now create additional states to add views to the application.
In the .qml file, you already created pointers to two additional states: State1 and State2. To create the states:
Note: When you set the expressions, drag and drop is disabled for the icon in Qt Quick Designer.
Click the rectangles to move the Qt logo from one rectangle to another.
Add transitions to define how the properties change when the Qt logo moves between states. The transitions apply animations to the Qt logo. For example, the Qt logo bounces back when it moves to the middleRightRect and eases into bottomLeftRect. Add the transitions in the code editor.
Click the rectangles to view the animated transitions.
When you have completed the steps, the main.qml file should look as follows:
import QtQuick 1.1 Rectangle { id: page width: 360 height: 360 color: "#343434" Image { id: icon x: 10 y: 20 source: "states.png" } Rectangle { id: topLeftRect y: 20 width: 64 height: 64 color: "#00000000" radius: 6 anchors.left: parent.left anchors.leftMargin: 10 anchors.top: parent.top anchors.topMargin: 20 border.color: "#808080" MouseArea { id: mousearea1 anchors.fill: parent onClicked: page.state = ' ' } } Rectangle { id: middleRightRect x: 13 y: 16 width: 64 height: 64 color: "#00000000" radius: 6 anchors.right: parent.right anchors.rightMargin: 10 anchors.verticalCenter: parent.verticalCenter border.color: "#808080" MouseArea { id: mousearea2 anchors.fill: parent onClicked: page.state = 'State1' } } Rectangle { id: bottomLeftRect y: 11 width: 64 height: 64 color: "#00000000" radius: 6 anchors.left: parent.left anchors.leftMargin: 10 anchors.bottom: parent.bottom anchors.bottomMargin: 20 border.color: "#808080" MouseArea { id: mousearea3 anchors.fill: parent onClicked: page.state = 'State2' } } states: [ State { name: "State1" PropertyChanges { target: icon x: middleRightRect.x y: middleRightRect.y } }, State { name: "State2" PropertyChanges { target: icon x: bottomLeftRect.x y: bottomLeftRect.y } } ] transitions: [ Transition { from: "*"; to: "State1" NumberAnimation { properties: "x,y"; duration: 1000 } }, Transition { from: "*"; to: "State2" NumberAnimation { properties: "x,y"; easing.type: Easing.InOutQuad; duration: 2000 } }, Transition { NumberAnimation { properties: "x,y"; duration: 200 } } ] }