qtquick-app-tutorial.qdoc 13.4 KB
Newer Older
1 2
/****************************************************************************
**
Eike Ziller's avatar
Eike Ziller committed
3 4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
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
**
**
** 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.
// **********************************************************************

/*!
26
    \contentspage {Qt Creator Manual}
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
27
    \previouspage creator-tutorials.html
28
    \example transitions
29
    \nextpage creator-writing-program.html
30

31
    \title Creating a Qt Quick Application
32

33
    This tutorial uses built-in QML types and illustrates basic concepts of
34
    \l{Qt Quick}.
35

36 37
    This tutorial describes how to use \QC to implement Qt Quick states and
    transitions. We
38 39
    create an application that displays a Qt logo that moves between three rectangles on the
    page when you click them.
40 41 42 43 44 45

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

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

Leena Miettinen's avatar
Leena Miettinen committed
46 47 48
    For tutorials that describe using Qt Quick Controls, see
    \l{Qt Quick Text Editor Guide} and \l{Qt Quick Controls - UI Forms}.

49 50 51 52
    \section1 Creating the Project

    \list 1

53
        \li Select \uicontrol{File > New File or Project > Application >
54
            Qt Quick Controls Application > Choose}.
55

56
        \li In the \uicontrol{Name} field, type \e {Transitions}.
57

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

62 63
        \li In the \uicontrol {Minimal required Qt version} field, select the Qt
            version to develop with.
64

65
            \note This page determines the set of files that the wizard
66
            generates and their contents. The instructions in this tutorial
67 68
            might not apply if you select the \uicontrol {With .ui.qml file}
            check box.
69

70
        \li Select \l{glossary-buildandrun-kit}{kits} for running and building your project,
71
            and then click \uicontrol{Next}.
72

73 74 75 76
            \note Kits are listed if they have been specified in \uicontrol Tools >
            \uicontrol Options > \uicontrol {Build & Run} > \uicontrol Kits (on Windows and Linux)
            or in \uicontrol {Qt Creator} > \uicontrol Preferences \uicontrol {Build & Run} >
            \uicontrol Kits (on OS X).
77

78
        \li Select \uicontrol Next in the following dialogs to use the default
79 80
            settings.

81 82
        \li Review the project settings, and click \uicontrol{Finish} (on Windows and
            Linux) or \uicontrol Done (on OS X) to create the project.
83

84
        \li Press \key {Ctrl+R} (or \key {Cmd+R}) to run the application.
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.

98
    To use the states.svg image in your application, you must copy it to the
99 100
    project directory (same subdirectory as the QML file) from the examples
    directory in the Qt installation directory. For example:
101
    \c {C:\Qt\Examples\Qt-5.4\declarative\animation\states}. The image appears
102
    in \uicontrol Resources. You can also use any other image or a QML
103
    type, instead.
104 105 106

    \list 1

107
        \li In the \uicontrol Projects view, double-click the main.qml file
108 109
            to open it in the code editor.

110
        \li Click \uicontrol Design to open the file in \QMLD.
111

Mitch Curtis's avatar
Mitch Curtis committed
112
            \image qmldesigner-tutorial-design-mode.png "Transitions project in Design Mode"
113

114 115
        \li In the \uicontrol Navigator, select \uicontrol Label and press
            \key Delete to delete it.
116

117
        \li Select \uicontrol ApplicationWindow to edit its properties.
118 119 120 121 122

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

        \list a

123
            \li In the \uicontrol Id field, enter \e page, to be able to reference the
124 125
                window from other places.

126 127 128 129 130
            \li In the \uicontrol Title field, type \e Transitions.

            \li In the \uicontrol Size field, set \uicontrol W and \uicontrol H
                to \e 330.

131
            \li In the \uicontrol Color field, set the window background color
132
                to \e #343434.
133 134 135

        \endlist

136
        \li In \uicontrol Library > \uicontrol Resources, select states.svg and
137 138 139 140 141 142
            drag and drop it to the canvas.

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

        \list a

143
            \li In the \uicontrol Id field, enter \e icon.
144

145 146
            \li In the \uicontrol Position field, set \uicontrol X to \e 10 and
                \uicontrol Y to \e 20.
147 148 149

        \endlist

150
        \li Right-click the resource file, qml.qrc, in the \uicontrol Projects
151
            view, and select \uicontrol {Open in Editor} to add states.svg to
152
            the resource file for deployment.
153

154 155
        \li Click \uicontrol Add > \uicontrol {Add File} and select
            \e states.svg.
156

157 158
        \li In the \uicontrol Design mode, \uicontrol Library view, \uicontrol {QML Types} tab,
            select \uicontrol Rectangle,
159 160 161 162 163 164
            drag and drop it to the canvas, and edit its properties.

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

        \list a

165
            \li In the \uicontrol Id field, enter \e topLeftRect.
166

167 168
            \li In the \uicontrol Size field, set \uicontrol W and \uicontrol H
                to \e 44, for the rectangle size to match the image size.
169

170
            \li In the \uicontrol Color field, click the
171
                \inlineimage qmldesigner-transparent-button.png
172
                (\uicontrol Transparent) button to make the rectangle transparent.
173

174 175
            \li In the \uicontrol Border field, set the border color to
                \e #808080.
176

177
            \li In the \uicontrol Rectangle group, \uicontrol Border field, set the border
178
                width to \e 1.
179

180 181
            \li In the \uicontrol Radius field, select \e 6 to create rounded
                corners for the rectangle.
182

183
            \li Click \uicontrol {Layout}, and then click the top and left anchor
184 185 186 187 188
                buttons to anchor the rectangle to the top left corner of the
                page.

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

189 190
            \li In the \uicontrol Margin field, select \e 20 for the top anchor
                and \e 10 for the left anchor.
191 192 193

        \endlist

194 195
        \li Drag and drop a \uicontrol {Mouse Area} type from the
            \uicontrol Library to \e topLeftRect in the \uicontrol Navigator.
196

197
        \li  Edit the \uicontrol {Mouse Area} properties:
198 199 200

        \list a

201
            \li Click \uicontrol {Layout}, and then click the
202
                \inlineimage qmldesigner-anchor-fill-screen.png
203
                (\uicontrol {Fill to Parent}) button to anchor the mouse area to the
204
                rectangle.
205

206
            \li In the code editor, edit the pointer to the clicked expression
207
                in the mouse area, as illustrated by the following code
208 209
                snippet:

210 211 212
                \quotefromfile transitions/main.qml
                \skipto MouseArea
                \printuntil }
213 214 215

                The expression sets the state to the base state and returns the
                image to its initial position.
216
                You will create stateGroup later.
217 218 219

        \endlist

220 221
            The qml.main file should now look as follows:

222 223
            \quotefromfile transitions/main.qml
            \skipto Window {
224
            \printuntil mouseArea1
225
            \printuntil }
226

227
        \li In the \uicontrol Navigator, copy topLeftRect (by pressing
228
            \key {Ctrl+C}) and paste it to the canvas twice (by pressing
229
            \key {Ctrl+V}). \QC renames the new instances of the type
230 231
            topLeftRect1 and topLeftRect2.

232
        \li  Select topLeftRect1 and edit its properties:
233 234 235

        \list a

236
            \li In the \uicontrol Id field, enter \e middleRightRect.
237

238
            \li In \uicontrol {Layout}, select the vertical center anchor button and
239 240 241
                then the right anchor button to
                anchor the rectangle to the middle right margin of the screen.

242 243
            \li In the \uicontrol Margin field, select \e 10 for the right
                anchor and \e 0 for the vertical center anchor.
244

245
            \li In the code editor, add a pointer to a clicked expression to the
246
                mouse area. The following expression sets the state to
247 248
                \e State1:

249
                \c {onClicked: stateGroup.state = 'State1'}
250 251 252 253 254

                You will create State1 later.

        \endlist

255
        \li Select topLeftRect2 and edit its properties:
256 257 258

        \list a

259
            \li In the \uicontrol Id field, enter \e bottomLeftRect.
260

261
            \li In \uicontrol {Layout}, select the bottom and left anchor buttons to
262 263
                anchor the rectangle to the bottom left margin of the screen.

264 265
            \li In the \uicontrol Margin field, select \e 20 for the bottom
                anchor and \e 10 for the left anchor.
266

267
            \li In the code editor, add a pointer to a clicked expression to the
268
                mouse area. The following expression sets the state to
269 270
                \e State2:

271
                \c {onClicked: stateGroup.state = 'State2'}
272 273 274

                You will create State2 later.

275 276
            The qml.main file should now look as follows:

277 278
            \quotefromfile transitions/main.qml
            \skipto Window {
279
            \printuntil mouseArea3
280 281
            \printuntil }
            \printuntil }
282

283 284
        \endlist

285
        \li Press \key {Ctrl+S} to save the changes.
286

287
        \li Press \key {Ctrl+R} to run the application.
288 289 290 291 292 293 294 295 296 297 298 299 300

    \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:
301 302 303
    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:
304 305 306

    \list 1

307
        \li Bind the position of the Qt logo to the
308 309 310 311 312
            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:

313 314 315
            \quotefromfile transitions/main.qml
            \skipto StateGroup {
            \printuntil ]
316

317
        \li Press \key {Ctrl+R} to run the application.
318 319 320 321 322 323 324

    \endlist

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

    \section1 Adding Animation to the View

325 326
    Add transitions inside the state group to define how the properties change
    when the Qt logo moves
327 328 329 330 331 332
    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

333
        \li In the code editor, add the following code to specify that when
334 335 336
            moving to State1, the x and y coordinates of the Qt logo change
            linearly over a duration of 1 second:

337 338 339
            \dots
            \skipto transitions
            \printuntil },
340

341
        \li You can use the Qt Quick toolbar for animation to change the easing
342 343 344 345
            curve type from linear to OutBounce:

        \list a

346
            \li Click \uicontrol NumberAnimation in the code editor to display the
347 348 349 350 351
                \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"

352
            \li In the \uicontrol Easing field, select \uicontrol Bounce.
353

354
            \li In the \uicontrol Subtype field, select \uicontrol Out.
355 356 357

        \endlist

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

362 363
            \dots
            \printuntil },
364

365
        \li Add the following code to specify that for any other state changes,
366 367 368
            the x and y coordinates of the Qt logo change linearly over a
            duration of 200 milliseconds:

369 370
            \dots
            \printuntil ]
371

372
        \li Press \key {Ctrl+R} to run the application.
373 374 375 376 377

    \endlist

    Click the rectangles to view the animated transitions.
*/