Doc: update the Qt Quick app tutorial
The wizard in 3.1 uses a Window QML type instead of a Rectangle, which means that states must be placed within a statusGroup. The example can no longer be built from Qt Quick 1 elements. Replaced \snippet commands with \quotefromfile commands. Change-Id: Ia2cec2b8b638913a2b9b2e27b36e6f2f1ffc4a49 Reviewed-by: Alessandro Portale <alessandro.portale@digia.com> Reviewed-by: Marco Bubke <marco.bubke@digia.com> Reviewed-by: Leena Miettinen <riitta-leena.miettinen@digia.com>
@@ -1,13 +1,9 @@
|
|||||||
//! [2]
|
import QtQuick 2.1
|
||||||
|
import QtQuick.Window 2.1
|
||||||
|
|
||||||
//! [1]
|
Window {
|
||||||
|
|
||||||
//! [0]
|
|
||||||
|
|
||||||
import QtQuick 2.0
|
|
||||||
|
|
||||||
Rectangle {
|
|
||||||
id: page
|
id: page
|
||||||
|
visible: true
|
||||||
width: 360
|
width: 360
|
||||||
height: 360
|
height: 360
|
||||||
color: "#343434"
|
color: "#343434"
|
||||||
@@ -21,11 +17,11 @@ Rectangle {
|
|||||||
|
|
||||||
Rectangle {
|
Rectangle {
|
||||||
id: topLeftRect
|
id: topLeftRect
|
||||||
y: 20
|
|
||||||
width: 64
|
width: 64
|
||||||
height: 64
|
height: 64
|
||||||
color: "#00000000"
|
color: "#00000000"
|
||||||
radius: 6
|
radius: 6
|
||||||
|
opacity: 1
|
||||||
anchors.left: parent.left
|
anchors.left: parent.left
|
||||||
anchors.leftMargin: 10
|
anchors.leftMargin: 10
|
||||||
anchors.top: parent.top
|
anchors.top: parent.top
|
||||||
@@ -33,95 +29,95 @@ Rectangle {
|
|||||||
border.color: "#808080"
|
border.color: "#808080"
|
||||||
|
|
||||||
MouseArea {
|
MouseArea {
|
||||||
id: mousearea1
|
id: mouseArea1
|
||||||
anchors.fill: parent
|
anchors.fill: parent
|
||||||
onClicked: page.state = ' '
|
onClicked: stateGroup.state = ' '
|
||||||
}
|
|
||||||
}
|
}
|
||||||
//! [0]
|
}
|
||||||
|
|
||||||
Rectangle {
|
Rectangle {
|
||||||
id: middleRightRect
|
id: middleRightRect
|
||||||
width: 64
|
width: 64
|
||||||
height: 64
|
height: 64
|
||||||
color: "#00000000"
|
color: "#00000000"
|
||||||
radius: 6
|
radius: 6
|
||||||
anchors.right: parent.right
|
anchors.right: parent.right
|
||||||
anchors.rightMargin: 10
|
anchors.rightMargin: 10
|
||||||
anchors.verticalCenter: parent.verticalCenter
|
anchors.verticalCenter: parent.verticalCenter
|
||||||
border.color: "#808080"
|
border.color: "#808080"
|
||||||
MouseArea {
|
|
||||||
id: mousearea2
|
MouseArea {
|
||||||
anchors.fill: parent
|
id: mouseArea2
|
||||||
onClicked: page.state = 'State1'
|
anchors.fill: parent
|
||||||
}
|
onClicked: stateGroup.state = 'State1'
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Rectangle {
|
Rectangle {
|
||||||
id: bottomLeftRect
|
id: bottomLeftRect
|
||||||
width: 64
|
width: 64
|
||||||
height: 64
|
height: 64
|
||||||
color: "#00000000"
|
color: "#00000000"
|
||||||
radius: 6
|
radius: 6
|
||||||
anchors.left: parent.left
|
border.width: 1
|
||||||
anchors.leftMargin: 10
|
anchors.leftMargin: 10
|
||||||
anchors.bottom: parent.bottom
|
anchors.bottom: parent.bottom
|
||||||
anchors.bottomMargin: 20
|
anchors.bottomMargin: 20
|
||||||
border.color: "#808080"
|
anchors.left: parent.left
|
||||||
MouseArea {
|
border.color: "#808080"
|
||||||
id: mousearea3
|
|
||||||
anchors.fill: parent
|
MouseArea {
|
||||||
onClicked: page.state = 'State2'
|
id: mouseArea3
|
||||||
}
|
anchors.fill: parent
|
||||||
|
onClicked: stateGroup.state = 'State2'
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//! [1]
|
StateGroup {
|
||||||
|
id: stateGroup
|
||||||
states: [
|
states: [
|
||||||
State {
|
State {
|
||||||
name: "State1"
|
name: "State1"
|
||||||
|
|
||||||
PropertyChanges {
|
PropertyChanges {
|
||||||
target: icon
|
target: icon
|
||||||
x: middleRightRect.x
|
x: middleRightRect.x
|
||||||
y: middleRightRect.y
|
y: middleRightRect.y
|
||||||
}
|
|
||||||
},
|
|
||||||
State {
|
|
||||||
name: "State2"
|
|
||||||
|
|
||||||
PropertyChanges {
|
|
||||||
target: icon
|
|
||||||
x: bottomLeftRect.x
|
|
||||||
y: bottomLeftRect.y
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
|
|
||||||
transitions: [
|
|
||||||
Transition {
|
|
||||||
from: "*"; to: "State1"
|
|
||||||
NumberAnimation {
|
|
||||||
easing.type: Easing.OutBounce
|
|
||||||
properties: "x,y";
|
|
||||||
duration: 1000
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Transition {
|
State {
|
||||||
from: "*"; to: "State2"
|
name: "State2"
|
||||||
NumberAnimation {
|
|
||||||
properties: "x,y";
|
PropertyChanges {
|
||||||
easing.type: Easing.InOutQuad;
|
target: icon
|
||||||
duration: 2000
|
x: bottomLeftRect.x
|
||||||
|
y: bottomLeftRect.y
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
Transition {
|
|
||||||
NumberAnimation {
|
|
||||||
properties: "x,y";
|
|
||||||
duration: 200
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
]
|
||||||
|
transitions: [
|
||||||
|
Transition {
|
||||||
|
from: "*"; to: "State1"
|
||||||
|
NumberAnimation {
|
||||||
|
easing.type: Easing.OutBounce
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//! [2]
|
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 105 KiB After Width: | Height: | Size: 104 KiB |
|
Before Width: | Height: | Size: 37 KiB After Width: | Height: | Size: 20 KiB |
|
Before Width: | Height: | Size: 41 KiB After Width: | Height: | Size: 99 KiB |
|
Before Width: | Height: | Size: 9.7 KiB |
@@ -1,31 +0,0 @@
|
|||||||
Item {
|
|
||||||
transitions: [
|
|
||||||
//! [first transition]
|
|
||||||
Transition {
|
|
||||||
from: "*"; to: "State1"
|
|
||||||
NumberAnimation {
|
|
||||||
properties: "x,y";
|
|
||||||
duration: 1000
|
|
||||||
}
|
|
||||||
},
|
|
||||||
//! [first transition]
|
|
||||||
//! [second transition]
|
|
||||||
Transition {
|
|
||||||
from: "*"; to: "State2"
|
|
||||||
NumberAnimation {
|
|
||||||
properties: "x,y";
|
|
||||||
easing.type: Easing.InOutQuad;
|
|
||||||
duration: 2000
|
|
||||||
}
|
|
||||||
},
|
|
||||||
//! [second transition]
|
|
||||||
//! [default transition]
|
|
||||||
Transition {
|
|
||||||
NumberAnimation {
|
|
||||||
properties: "x,y";
|
|
||||||
duration: 200
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
//! [default transition]
|
|
||||||
}
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
Item {
|
|
||||||
//! [states]
|
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
//! [states]
|
|
||||||
}
|
|
||||||
@@ -33,8 +33,8 @@
|
|||||||
This tutorial uses built-in QML types and illustrates basic concepts of
|
This tutorial uses built-in QML types and illustrates basic concepts of
|
||||||
\l{Qt Quick}.
|
\l{Qt Quick}.
|
||||||
|
|
||||||
This tutorial describes how to use \QC to implement Qt states and transitions. We use
|
This tutorial describes how to use \QC to implement Qt Quick states and
|
||||||
\l{Animation}{Qt example code} to
|
transitions. We
|
||||||
create an application that displays a Qt logo that moves between three rectangles on the
|
create an application that displays a Qt logo that moves between three rectangles on the
|
||||||
page when you click them.
|
page when you click them.
|
||||||
|
|
||||||
@@ -57,11 +57,7 @@
|
|||||||
Windows and Linux) or \gui Continue (on Mac OS).
|
Windows and Linux) or \gui Continue (on Mac OS).
|
||||||
|
|
||||||
\li In the \gui {Qt Quick component set} field, select
|
\li In the \gui {Qt Quick component set} field, select
|
||||||
\gui {Qt Quick 2.0}.
|
\gui {Qt Quick 2.1}.
|
||||||
|
|
||||||
\note The QML types used in this example are also supported in
|
|
||||||
Qt Quick 1.1. To create this example application for platforms that
|
|
||||||
run Qt 4, select \gui {Qt Quick 1.1}.
|
|
||||||
|
|
||||||
\li Select \l{glossary-buildandrun-kit}{kits} for running and building your project,
|
\li Select \l{glossary-buildandrun-kit}{kits} for running and building your project,
|
||||||
and then click \gui{Next}.
|
and then click \gui{Next}.
|
||||||
@@ -92,7 +88,8 @@
|
|||||||
To use the states.png image in your application, you must copy it to the
|
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
|
project directory (same subdirectory as the QML file) from the examples
|
||||||
directory in the Qt installation directory. For example:
|
directory in the Qt installation directory. For example:
|
||||||
\c {C:\Qt\Qt5.0.1\5.0.1\msvc2010\examples\declarative\animation\states}. The image appears
|
\c {C:\Qt\Qt5.3.0\5.3.0\msvc2010\examples\declarative\animation\states}. The
|
||||||
|
image appears
|
||||||
in the \gui Resources pane. You can also use any other image or a QML
|
in the \gui Resources pane. You can also use any other image or a QML
|
||||||
type, instead.
|
type, instead.
|
||||||
|
|
||||||
@@ -108,18 +105,22 @@
|
|||||||
\li In the \gui Navigator pane, select \gui Text and press \key Delete to
|
\li In the \gui Navigator pane, select \gui Text and press \key Delete to
|
||||||
delete it.
|
delete it.
|
||||||
|
|
||||||
\li Select \gui Rectangle to edit its properties.
|
\li Select \gui Window to edit its properties.
|
||||||
|
|
||||||
\image qmldesigner-tutorial-page.png "Page properties"
|
\image qmldesigner-tutorial-page.png "Page properties"
|
||||||
|
|
||||||
\list a
|
\list a
|
||||||
|
|
||||||
\li In the \gui Id field, enter \e page, to be able to reference the
|
\li In the \gui Id field, enter \e page, to be able to reference the
|
||||||
rectangle from other places.
|
window from other places.
|
||||||
|
|
||||||
\li In the \gui Color field, set the color to #343434.
|
\li In the code editor, set the window background color to #343434:
|
||||||
|
|
||||||
\li In the code editor, delete the \c {Qt.quit();} command.
|
\quotefromfile transitions/main.qml
|
||||||
|
\skipto Window {
|
||||||
|
\printuntil color
|
||||||
|
|
||||||
|
\li Delete the \c {Qt.quit();} command.
|
||||||
|
|
||||||
\endlist
|
\endlist
|
||||||
|
|
||||||
@@ -136,7 +137,13 @@
|
|||||||
|
|
||||||
\endlist
|
\endlist
|
||||||
|
|
||||||
\li In the \gui Library view, \gui {QML Types} tab, select \gui Rectangle,
|
\li Double-click the resource file, qml.qrc, in the \gui Projects view
|
||||||
|
to add states.png to the resource file for deployment.
|
||||||
|
|
||||||
|
\li Click \gui Add and select states.png.
|
||||||
|
|
||||||
|
\li In the \gui Design mode, \gui Library view, \gui {QML Types} tab,
|
||||||
|
select \gui Rectangle,
|
||||||
drag and drop it to the canvas, and edit its properties.
|
drag and drop it to the canvas, and edit its properties.
|
||||||
|
|
||||||
\image qmldesigner-tutorial-topleftrect.png "Rectangle properties"
|
\image qmldesigner-tutorial-topleftrect.png "Rectangle properties"
|
||||||
@@ -194,21 +201,24 @@
|
|||||||
in the mouse area, as illustrated by the following code
|
in the mouse area, as illustrated by the following code
|
||||||
snippet:
|
snippet:
|
||||||
|
|
||||||
\qml
|
\quotefromfile transitions/main.qml
|
||||||
MouseArea {
|
\skipto MouseArea
|
||||||
anchors.fill: parent
|
\printuntil }
|
||||||
onClicked: page.state = ''
|
|
||||||
}
|
|
||||||
\endqml
|
|
||||||
|
|
||||||
The expression sets the state to the base state and returns the
|
The expression sets the state to the base state and returns the
|
||||||
image to its initial position.
|
image to its initial position.
|
||||||
|
You will create stateGroup later.
|
||||||
|
|
||||||
\endlist
|
\endlist
|
||||||
|
|
||||||
The qml.main file should now look as follows:
|
The qml.main file should now look as follows:
|
||||||
|
|
||||||
\snippet transitions/main.qml 0
|
\quotefromfile transitions/main.qml
|
||||||
|
\skipto Window {
|
||||||
|
\printuntil onClicked
|
||||||
|
\printuntil }
|
||||||
|
\printuntil }
|
||||||
|
\printuntil }
|
||||||
|
|
||||||
\li In the \gui Navigator pane, copy topLeftRect (by pressing
|
\li In the \gui Navigator pane, copy topLeftRect (by pressing
|
||||||
\key {Ctrl+C}) and paste it to the canvas twice (by pressing
|
\key {Ctrl+C}) and paste it to the canvas twice (by pressing
|
||||||
@@ -232,7 +242,7 @@
|
|||||||
mouse area. The following expression sets the state to
|
mouse area. The following expression sets the state to
|
||||||
\e State1:
|
\e State1:
|
||||||
|
|
||||||
\c {onClicked: page.state = 'State1'}
|
\c {onClicked: stateGroup.state = 'State1'}
|
||||||
|
|
||||||
You will create State1 later.
|
You will create State1 later.
|
||||||
|
|
||||||
@@ -254,13 +264,18 @@
|
|||||||
mouse area. The following expression sets the state to
|
mouse area. The following expression sets the state to
|
||||||
\e State2:
|
\e State2:
|
||||||
|
|
||||||
\c {onClicked: page.state = 'State2'}
|
\c {onClicked: stateGroup.state = 'State2'}
|
||||||
|
|
||||||
You will create State2 later.
|
You will create State2 later.
|
||||||
|
|
||||||
The qml.main file should now look as follows:
|
The qml.main file should now look as follows:
|
||||||
|
|
||||||
\snippet transitions/main.qml 1
|
\quotefromfile transitions/main.qml
|
||||||
|
\skipto Window {
|
||||||
|
\printuntil State2
|
||||||
|
\printuntil }
|
||||||
|
\printuntil }
|
||||||
|
\printuntil }
|
||||||
|
|
||||||
\endlist
|
\endlist
|
||||||
|
|
||||||
@@ -280,26 +295,21 @@
|
|||||||
\section1 Adding Views
|
\section1 Adding Views
|
||||||
|
|
||||||
In the .qml file, you already created pointers to two additional states:
|
In the .qml file, you already created pointers to two additional states:
|
||||||
State1 and State2. To create the states:
|
State1 and State2. You cannot use the \QMLD to add states for a Window QML
|
||||||
|
type. Use the code editor to add the states inside a StateGroup QML type and
|
||||||
|
refer to them by using the id of the state group:
|
||||||
|
|
||||||
\list 1
|
\list 1
|
||||||
|
|
||||||
\li Click the empty slot in the \gui States pane to create State1.
|
\li Bind the position of the Qt logo to the
|
||||||
|
|
||||||
\li Click the empty slot in the \gui States pane to create State2.
|
|
||||||
|
|
||||||
\li In the code editor, bind the position of the Qt logo to the
|
|
||||||
rectangle to make sure that the logo is displayed within the
|
rectangle to make sure that the logo is displayed within the
|
||||||
rectangle when the view is scaled on different sizes of screens. Set
|
rectangle when the view is scaled on different sizes of screens. Set
|
||||||
expressions for the x and y properties, as illustrated by the
|
expressions for the x and y properties, as illustrated by the
|
||||||
following code snippet:
|
following code snippet:
|
||||||
|
|
||||||
\snippet qml/states-properties.qml states
|
\quotefromfile transitions/main.qml
|
||||||
|
\skipto StateGroup {
|
||||||
\image qmldesigner-tutorial-state1.png "States"
|
\printuntil ]
|
||||||
|
|
||||||
\note When you set the expressions, drag and drop is disabled for
|
|
||||||
the icon in \QMLD.
|
|
||||||
|
|
||||||
\li Press \key {Ctrl+R} to run the application.
|
\li Press \key {Ctrl+R} to run the application.
|
||||||
|
|
||||||
@@ -309,7 +319,8 @@
|
|||||||
|
|
||||||
\section1 Adding Animation to the View
|
\section1 Adding Animation to the View
|
||||||
|
|
||||||
Add transitions to define how the properties change when the Qt logo moves
|
Add transitions inside the state group to define how the properties change
|
||||||
|
when the Qt logo moves
|
||||||
between states. The transitions apply animations to the Qt logo. For example,
|
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
|
the Qt logo bounces back when it moves to the middleRightRect and eases into
|
||||||
bottomLeftRect. Add the transitions in the code editor.
|
bottomLeftRect. Add the transitions in the code editor.
|
||||||
@@ -320,7 +331,9 @@
|
|||||||
moving to State1, the x and y coordinates of the Qt logo change
|
moving to State1, the x and y coordinates of the Qt logo change
|
||||||
linearly over a duration of 1 second:
|
linearly over a duration of 1 second:
|
||||||
|
|
||||||
\snippet qml/list-of-transitions.qml first transition
|
\dots
|
||||||
|
\skipto transitions
|
||||||
|
\printuntil },
|
||||||
|
|
||||||
\li You can use the Qt Quick toolbar for animation to change the easing
|
\li You can use the Qt Quick toolbar for animation to change the easing
|
||||||
curve type from linear to OutBounce:
|
curve type from linear to OutBounce:
|
||||||
@@ -343,13 +356,15 @@
|
|||||||
and y coordinates of the Qt logo change over a duration of 2
|
and y coordinates of the Qt logo change over a duration of 2
|
||||||
seconds, and an InOutQuad easing function is used:
|
seconds, and an InOutQuad easing function is used:
|
||||||
|
|
||||||
\snippet qml/list-of-transitions.qml second transition
|
\dots
|
||||||
|
\printuntil },
|
||||||
|
|
||||||
\li Add the following code to specify that for any other state changes,
|
\li Add the following code to specify that for any other state changes,
|
||||||
the x and y coordinates of the Qt logo change linearly over a
|
the x and y coordinates of the Qt logo change linearly over a
|
||||||
duration of 200 milliseconds:
|
duration of 200 milliseconds:
|
||||||
|
|
||||||
\snippet qml/list-of-transitions.qml default transition
|
\dots
|
||||||
|
\printuntil ]
|
||||||
|
|
||||||
\li Press \key {Ctrl+R} to run the application.
|
\li Press \key {Ctrl+R} to run the application.
|
||||||
|
|
||||||
@@ -361,6 +376,8 @@
|
|||||||
|
|
||||||
When you have completed the steps, the main.qml file should look as follows:
|
When you have completed the steps, the main.qml file should look as follows:
|
||||||
|
|
||||||
\snippet transitions/main.qml 2
|
\quotefromfile transitions/main.qml
|
||||||
|
\skipto Window {
|
||||||
|
\printuntil /^\}/
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|||||||