qtquick-app-tutorial.qdoc 13.1 KB
Newer Older
1 2
/****************************************************************************
**
3
** Copyright (c) 2014 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
**
**
** 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
    \page creator-qml-application.html
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 46 47 48 49

    \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

50
        \li Select \gui{File > New File or Project > Application >
51
            Qt Quick Application > Choose}.
52

53
        \li In the \gui{Name} field, type \b {Transitions}.
54

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

59
        \li In the \gui {Qt Quick component set} field, select
60
            \gui {Qt Quick 2.1}.
61

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

65
            \note Kits are listed if they have been specified in \gui Tools >
66 67 68
            \gui Options > \gui {Build & Run} > \gui Kits (on Windows and Linux)
            or in \gui {Qt Creator} > \gui Preferences \gui {Build & Run} >
            \gui Kits (on OS X).
69

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

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

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

    \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:
93 94
    \c {C:\Qt\Qt5.3.0\5.3.0\msvc2010\examples\declarative\animation\states}. The
    image appears
95
    in \gui Resources. You can also use any other image or a QML
96
    type, instead.
97 98 99

    \list 1

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

103
        \li Click \gui Design to open the file in \QMLD.
104 105 106

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

107
        \li In the \gui Navigator, select \gui Text and press \key Delete to
108 109
            delete it.

110
        \li Select \gui Window to edit its properties.
111 112 113 114 115

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

        \list a

116
            \li In the \gui Id field, enter \e page, to be able to reference the
117 118 119
                window from other places.

            \li In the code editor, set the window background color to #343434:
120

121 122 123
            \quotefromfile transitions/main.qml
            \skipto Window {
            \printuntil color
124

125
            \li Delete the \c {Qt.quit();} command.
126 127 128

        \endlist

129
        \li In \gui Library > \gui Resources, select states.png and
130 131 132 133 134 135
            drag and drop it to the canvas.

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

        \list a

136
            \li In the \gui Id field, enter \e icon.
137

138
            \li In the \gui Position field, set \gui X to 10 and \gui Y to 20.
139 140 141

        \endlist

142 143 144 145 146 147 148
        \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,
149 150 151 152 153 154
            drag and drop it to the canvas, and edit its properties.

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

        \list a

155
            \li In the \gui Id field, enter \e topLeftRect.
156

157
            \li In the \gui Size field, set \gui W and \gui H to 64, for the
158 159
                rectangle size to match the image size.

160
            \li In the \gui Color field, click the
161
                \inlineimage qmldesigner-transparent-button.png
162
                (\gui Transparent) button to make the rectangle transparent.
163

164
            \li In the \gui Border field, set the border color to #808080.
165

166
            \li In the \gui Rectangle group, \gui Border field, set the border
167 168 169 170 171 172
                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
173
                (\gui {Solid Color}) button.
174

175
            \li In the \gui Radius field, select 6 to create rounded corners for
176 177
                the rectangle.

178
            \li Click \gui {Layout}, and then click the top and left anchor
179 180 181 182 183
                buttons to anchor the rectangle to the top left corner of the
                page.

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

184
            \li In the \gui Margin field, select 20 for the top anchor and 10
185 186 187 188
                for the left anchor.

        \endlist

189
        \li In the \gui Navigator, drag and drop the \gui {Mouse Area}
190
            from \e page to \e topLeftRect to make it apply only to the
191 192
            rectangle and not to the whole page.

193
        \li  Edit the \gui {Mouse Area} properties:
194 195 196

        \list a

197
            \li Click \gui {Layout}, and then click the
198
                \inlineimage qmldesigner-anchor-fill-screen.png
199 200
                (\gui {Fill to Parent}) button to anchor the mouse area to the
                rectangle.
201

202
            \li In the code editor, edit the pointer to the clicked expression
203
                in the mouse area, as illustrated by the following code
204 205
                snippet:

206 207 208
                \quotefromfile transitions/main.qml
                \skipto MouseArea
                \printuntil }
209 210 211

                The expression sets the state to the base state and returns the
                image to its initial position.
212
                You will create stateGroup later.
213 214 215

        \endlist

216 217
            The qml.main file should now look as follows:

218 219 220 221 222 223
            \quotefromfile transitions/main.qml
            \skipto Window {
            \printuntil onClicked
            \printuntil }
            \printuntil }
            \printuntil }
224

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

230
        \li  Select topLeftRect1 and edit its properties:
231 232 233

        \list a

234
            \li In the \gui Id field, enter \e middleRightRect.
235

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

240
            \li In the \gui Margin field, select 10 for the right anchor and 0
241 242
                for the vertical center anchor.

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

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

                You will create State1 later.

        \endlist

253
        \li Select topLeftRect2 and edit its properties:
254 255 256

        \list a

257
            \li In the \gui Id field, enter \e bottomLeftRect.
258

259
            \li In \gui {Layout}, select the bottom and left anchor buttons to
260 261
                anchor the rectangle to the bottom left margin of the screen.

262
            \li In the \gui Margin field, select 20 for the bottom anchor and 10
263 264
                for the left anchor.

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

269
                \c {onClicked: stateGroup.state = 'State2'}
270 271 272

                You will create State2 later.

273 274
            The qml.main file should now look as follows:

275 276 277 278 279 280
            \quotefromfile transitions/main.qml
            \skipto Window {
            \printuntil State2
            \printuntil }
            \printuntil }
            \printuntil }
281

282 283
        \endlist

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

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

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

    \list 1

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

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

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

    \endlist

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

    \section1 Adding Animation to the View

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

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

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

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

        \list a

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

351
            \li In the \gui Easing field, select \gui Bounce.
352

353
            \li In the \gui Subtype field, select \gui Out.
354 355 356

        \endlist

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

361 362
            \dots
            \printuntil },
363

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

368 369
            \dots
            \printuntil ]
370

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

    \endlist

    Click the rectangles to view the animated transitions.

377 378 379 380
    \section1 Example Code

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

381 382 383
            \quotefromfile transitions/main.qml
            \skipto Window {
            \printuntil /^\}/
384

385
*/