qtquick-app-tutorial.qdoc 13.7 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
    \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

    \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 Application > Choose}.
55

56
        \li In the \uicontrol{Name} field, type \b {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 {Qt Quick component set} field, select
            \uicontrol {Qt Quick 2.1}.
64

65 66 67 68 69 70
            \note This selection determines the set of files that the wizard
            generates and their contents. The instructions in this tutorial
            might not apply if you select some other component set, such as Qt
            Quick 2.4. The wizard indicates which Qt version each component set
            requires at minimum.

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

74 75 76 77
            \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).
78

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

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

85
        \li Press \key {Ctrl+R} (or \key {Cmd+R}) to run the application.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

    \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:
102
    \c {C:\Qt\Examples\Qt-5.4\declarative\animation\states}. The image appears
103
    in \uicontrol Resources. You can also use any other image or a QML
104
    type, instead.
105 106 107

    \list 1

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

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

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

115 116
        \li In the \uicontrol Navigator, select \uicontrol MouseArea and
            \uicontrol Text and press \key Delete to delete them.
117

118
        \li Select \uicontrol Window to edit its properties.
119 120 121 122 123

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

        \list a

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

127 128
            \li In the \uicontrol Color field, set the window background color
                to #343434.
129 130 131

        \endlist

132
        \li In \uicontrol Library > \uicontrol Resources, select states.png and
133 134 135 136 137 138
            drag and drop it to the canvas.

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

        \list a

139
            \li In the \uicontrol Id field, enter \e icon.
140

141
            \li In the \uicontrol Position field, set \uicontrol X to 10 and \uicontrol Y to 20.
142 143 144

        \endlist

145 146 147
        \li Right-click the resource file, qml.qrc, in the \uicontrol Projects
            view, and select \uicontrol {Open in Editor} to add states.png to
            the resource file for deployment.
148

149
        \li Click \uicontrol Add and select states.png.
150

151 152
        \li In the \uicontrol Design mode, \uicontrol Library view, \uicontrol {QML Types} tab,
            select \uicontrol Rectangle,
153 154 155 156 157 158
            drag and drop it to the canvas, and edit its properties.

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

        \list a

159
            \li In the \uicontrol Id field, enter \e topLeftRect.
160

161
            \li In the \uicontrol Size field, set \uicontrol W and \uicontrol H to 64, for the
162 163
                rectangle size to match the image size.

164
            \li In the \uicontrol Color field, click the
165
                \inlineimage qmldesigner-transparent-button.png
166
                (\uicontrol Transparent) button to make the rectangle transparent.
167

168
            \li In the \uicontrol Border field, set the border color to #808080.
169

170
            \li In the \uicontrol Rectangle group, \uicontrol Border field, set the border
171 172
                width to 1.

173
                \note If the \uicontrol Border field does not appear after you set the
174 175 176
                border color, try setting the border color to solid by clicking
                the
                \inlineimage qmldesigner-solid-color-button.png
177
                (\uicontrol {Solid Color}) button.
178

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

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

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

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

        \endlist

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

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

        \list a

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

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

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

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

        \endlist

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

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

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

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

        \list a

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

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

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

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

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

                You will create State1 later.

        \endlist

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

        \list a

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

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

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

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

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

                You will create State2 later.

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

276 277
            \quotefromfile transitions/main.qml
            \skipto Window {
278
            \printuntil mouseArea3
279 280
            \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 \uicontrol 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 \uicontrol Easing field, select \uicontrol Bounce.
352

353
            \li In the \uicontrol Subtype field, select \uicontrol 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
*/