qtquick-app-tutorial.qdoc 12.7 KB
Newer Older
1 2
/****************************************************************************
**
3
** Copyright (c) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
**
**
** GNU Free Documentation License
**
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
** Foundation and appearing in the file included in the packaging of this
** file.
**
**
****************************************************************************/

// **********************************************************************
// NOTE: the sections are not ordered by their logical order to avoid
// reshuffling the file each time the index order changes (i.e., often).
// Run the fixnavi.pl script to adjust the links to the index order.
// **********************************************************************

/*!
    \contentspage index.html
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
27
    \previouspage creator-tutorials.html
28
    \page creator-qml-application.html
29
    \nextpage creator-writing-program.html
30

31
    \title Creating a Qt Quick 1 Application
32

33 34
    \note We recommend that you use Qt Quick 2 to develop new applications for
    platforms that run Qt 5.
35

36
    This tutorial uses built-in QML elements and illustrates basic concepts of
37
    \l {http://qt-project.org/doc/qt-4.8/qtquick.html}{Qt Quick 1}.
38 39

    This tutorial describes how to use the \QC to implement the
40
    \l{http://qt-project.org/doc/qt-4.8/declarative-animation-states.html}
41 42 43 44 45 46 47 48 49 50 51 52 53
    {states and transitions example application}. The example application
     displays a Qt logo that moves between three rectangles on the page when you
    click them.

    \image qmldesigner-tutorial.png "States and transitions example"

    For more information about using \QMLD, see
    \l{Developing Qt Quick Applications}.

    \section1 Creating the Project

    \list 1

54
        \li Select \gui{File > New File or Project > Applications >
55
            Qt Quick 1 Application (Built-in Elements) > Choose}.
56

57
        \li In the \gui{Name} field, type \b {Transitions}.
58

59
        \li In the \gui {Create in} field, enter the path for the project files.
60 61
            For example, \c {C:\Qt\examples}, and then click \gui{Next} (on
            Windows and Linux) or \gui Continue (on Mac OS).
62

63
        \li Select \l{glossary-buildandrun-kit}{kits} for running and building your project,
64
            and then click \gui{Next}.
65

66 67
            \note Kits are listed if they have been specified in \gui Tools >
            \gui Options > \gui {Build & Run} > \gui Kits.
68

69
        \li Select \gui Next in the following dialogs to use the default
70 71
            settings.

72
        \li Review the project settings, and click \gui{Finish} (on Windows and
73
            Linux) or \gui Done (on Mac OS) to create the project.
74

75
        \li Press \key {Ctrl+R} (or \key {Cmd+R})to run the application.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

    \endlist

    \QC generates a default QML file that you can modify to create the main view
    of the application.

    \image qmldesigner-tutorial-project.png "Transitions project in Edit mode"

    \section1 Creating the Main View

    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 {C:\QtSDK\Examples\4.7\declarative\animation\states}. The image appears
    in the \gui Resources pane. You can also use any other image or a QML
    element, instead.

    \list 1

98
        \li In the \gui Projects view, double-click the main.qml file
99 100
            to open it in the code editor.

101
        \li Click \gui Design to open the file in \QMLD.
102 103 104

            \image qmldesigner-tutorial-desing-mode.png "Transitions project in Design Mode"

105
        \li In the \gui Navigator pane, select \gui Text and press \key Delete to
106 107
            delete it.

108
        \li Select \gui Rectangle to edit its properties.
109 110 111 112 113

            \image qmldesigner-tutorial-page.png "Page properties"

        \list a

114
            \li In the \gui Id field, enter \e page, to be able to reference the
115 116
                rectangle from other places.

117
            \li In the \gui Colors group, \gui Rectangle field, set the color to
118 119 120 121
                #343434.

        \endlist

122
        \li In the \gui Library view, \gui Resources tab, select states.png and
123 124 125 126 127 128
            drag and drop it to the canvas.

            \image qmldesigner-tutorial-user-icon.png "Image properties"

        \list a

129
            \li In the \gui Id field, enter \e icon.
130

131
            \li In the \gui Position field, set \gui X to 10 and \gui Y to 20.
132 133 134

        \endlist

135
        \li In the \gui Library view, \gui Items tab, select \gui Rectangle,
136 137 138 139 140 141
            drag and drop it to the canvas, and edit its properties.

            \image qmldesigner-tutorial-topleftrect.png "Rectangle properties"

        \list a

142
            \li In the \gui Id field, enter \e topLeftRect.
143

144
            \li In the \gui Size field, set \gui W and \gui H to 64, for the
145 146
                rectangle size to match the image size.

147
            \li In the \gui Colors group, \gui Rectangle field, click the
148
                \inlineimage qmldesigner-transparent-button.png
149
                (\gui Transparent) button to make the rectangle transparent.
150

151
            \li In the \gui Border field, set the border color to #808080.
152

153
            \li In the \gui Rectangle group, \gui Border field, set the border
154 155 156 157 158 159
                width to 1.

                \note If the \gui Border field does not appear after you set the
                border color, try setting the border color to solid by clicking
                the
                \inlineimage qmldesigner-solid-color-button.png
160
                (\gui {Solid Color}) button.
161

162
            \li In the \gui Radius field, select 6 to create rounded corners for
163 164
                the rectangle.

165
            \li Click \gui {Layout}, and then click the top and left anchor
166 167 168 169 170
                buttons to anchor the rectangle to the top left corner of the
                page.

                \image qmldesigner-tutorial-topleftrect-layout.png "Layout tab"

171
            \li In the \gui Margin field, select 20 for the top anchor and 10
172 173 174 175
                for the left anchor.

        \endlist

176
        \li In the \gui Navigator pane, drag and drop the \gui {Mouse Area}
177 178 179
            element from \e page to \e topLeftRect to make it apply only to the
            rectangle and not to the whole page.

180
        \li  Edit \gui {Mouse Area} properties:
181 182 183

        \list a

184
            \li Click \gui {Layout}, and then click the
185
                \inlineimage qmldesigner-anchor-fill-screen.png
186 187
                (\gui {Fill to Parent}) button to anchor the mouse area to the
                rectangle.
188

189
            \li In the code editor, edit the pointer to the clicked expression
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
                in the mouse area element, as illustrated by the following code
                snippet:

                \qml
                MouseArea {
                    anchors.fill: parent
                    onClicked: page.state = ''
                }
                \endqml

                The expression sets the state to the base state and returns the
                image to its initial position.

        \endlist

205 206
            The qml.main file should now look as follows:

207
            \snippet transitions/qml/Transitions/main.qml 0
208

209
        \li In the \gui Navigator pane, copy topLeftRect (by pressing
210 211 212 213
            \key {Ctrl+C}) and paste it to the canvas twice (by pressing
            \key {Ctrl+V}). \QC renames the new instances of the element
            topLeftRect1 and topLeftRect2.

214
        \li  Select topLeftRect1 and edit its properties:
215 216 217

        \list a

218
            \li In the \gui Id field, enter \e middleRightRect.
219

220
            \li In \gui {Layout}, select the vertical center anchor button and
221 222 223
                then the right anchor button to
                anchor the rectangle to the middle right margin of the screen.

224
            \li In the \gui Margin field, select 10 for the right anchor and 0
225 226
                for the vertical center anchor.

227
            \li In the code editor,add a pointer to a clicked expression to the
228 229 230 231 232 233 234 235 236
                mouse area element. The following expression sets the state to
                \e State1:

                \c {onClicked: page.state = 'State1'}

                You will create State1 later.

        \endlist

237
        \li Select topLeftRect2 and edit its properties:
238 239 240

        \list a

241
            \li In the \gui Id field, enter \e bottomLeftRect.
242

243
            \li In \gui {Layout}, select the bottom and left anchor buttons to
244 245
                anchor the rectangle to the bottom left margin of the screen.

246
            \li In the \gui Margin field, select 20 for the bottom anchor and 10
247 248
                for the left anchor.

249
            \li In the code editor, add a pointer to a clicked expression to the
250 251 252 253 254 255 256
                mouse area element. The following expression sets the state to
                \e State2:

                \c {onClicked: page.state = 'State2'}

                You will create State2 later.

257 258
            The qml.main file should now look as follows:

259
            \snippet transitions/qml/Transitions/main.qml 1
260

261 262
        \endlist

263
        \li Press \key {Ctrl+S} to save the changes.
264

265
        \li Press \key {Ctrl+R} to run the application.
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282

    \endlist

    \image qmldesigner-tutorial.png "States and transitions example"

    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.

    \section1 Adding Views

    In the .qml file, you already created pointers to two additional states:
    State1 and State2. To create the states:

    \list 1

283
        \li Click the empty slot in the \gui States pane to create State1.
284

285
        \li Click the empty slot in the \gui States pane to create State2.
286

287
        \li In the code editor, bind the position of the Qt logo to the
288 289 290 291 292
            rectangle to make sure that the logo is displayed within the
            rectangle when the view is scaled on different sizes of screens. Set
            expressions for the x and y properties, as illustrated by the
            following code snippet:

Leena Miettinen's avatar
Leena Miettinen committed
293
            \snippet qml/states-properties.qml states
294 295 296 297 298 299

            \image qmldesigner-tutorial-state1.png "States"

            \note When you set the expressions, drag and drop is disabled for
            the icon in \QMLD.

300
        \li Press \key {Ctrl+R} to run the application.
301 302 303 304 305 306 307 308 309 310 311 312 313 314

    \endlist

    Click the rectangles to move the Qt logo from one rectangle to another.

    \section1 Adding Animation to the View

    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.

    \list 1

315
        \li In the code editor, add the following code to specify that when
316 317 318
            moving to State1, the x and y coordinates of the Qt logo change
            linearly over a duration of 1 second:

Leena Miettinen's avatar
Leena Miettinen committed
319
            \snippet qml/list-of-transitions.qml first transition
320

321
        \li You can use the Qt Quick toolbar for animation to change the easing
322 323 324 325
            curve type from linear to OutBounce:

        \list a

326
            \li Click \gui NumberAnimation in the code editor to display the
327 328 329 330 331
                \inlineimage qml-toolbar-indicator.png
                icon, and then click the icon to open the toolbar:

                \image qmldesigner-tutorial-quick-toolbar.png "Qt Quick toolbar for animation"

332
            \li In the \gui Easing field, select \gui Bounce.
333

334
            \li In the \gui Subtype field, select \gui Out.
335 336 337

        \endlist

338
        \li Add the following code to specify that when moving to State2, the x
339 340 341
            and y coordinates of the Qt logo change over a duration of 2
            seconds, and an InOutQuad easing function is used:

Leena Miettinen's avatar
Leena Miettinen committed
342
            \snippet qml/list-of-transitions.qml second transition
343

344
        \li Add the following code to specify that for any other state changes,
345 346 347
            the x and y coordinates of the Qt logo change linearly over a
            duration of 200 milliseconds:

Leena Miettinen's avatar
Leena Miettinen committed
348
            \snippet qml/list-of-transitions.qml default transition
349

350
        \li Press \key {Ctrl+R} to run the application.
351 352 353 354 355

    \endlist

    Click the rectangles to view the animated transitions.

356 357 358 359
    \section1 Example Code

    When you have completed the steps, the main.qml file should look as follows:

360
    \snippet transitions/qml/Transitions/main.qml 2
361

362
*/