Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
firststepsqml.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2016 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
/*!
4
\page qmlfirststeps.html
5
\meta {keywords} {qmltopic}
6
\title First Steps with QML
7
\brief Basic QML application development examples
8
9
\section1 Creating a QML Document
10
11
A QML document defines a hierarchy of objects with a highly-readable,
12
structured layout. Every QML document consists of two parts: an imports
13
section and an object declaration section. The types and functionality most
14
common to user interfaces are provided in the \c{QtQuick}
15
import.
16
17
\section2 Importing and Using the QtQuick Module
18
19
To use the \l{Qt Quick} module, a QML document needs to
20
import it. The import syntax looks like this:
21
22
\qml
23
import QtQuick
24
\endqml
25
26
The types and functionality that \l{Qt Quick} provides can now
27
be used in the QML document!
28
29
\section2 Defining an Object Hierarchy
30
31
The object declaration in a QML document defines what will be displayed in the
32
visual scene. \l{Qt Quick} provides the basic building blocks
33
for all user interfaces, such as the objects for displaying images and text and
34
for handling user input.
35
36
A simple object declaration might be a colored window with some text centered
37
in it:
38
39
\qml
40
Window {
41
width: 640
42
height: 480
43
visible: true
44
color: "red"
45
46
Text {
47
anchors.centerIn: parent
48
text: "Hello, World!"
49
}
50
}
51
\endqml
52
53
This defines an object hierarchy with a root \l Window object
54
which has a child \l Text object. The \c parent of the \l Text object is
55
automatically set to the \l Window, and similarly, the \l Text object is
56
added to the \c children property of the \l Window object, by QML.
57
58
\section2 Putting it All Together
59
60
The \l Window and \l Text types used in the above example are both provided
61
by the \c{QtQuick} import. Putting the import and object declaration
62
together, we get a complete QML document:
63
64
\qml
65
import QtQuick
66
67
Window {
68
width: 640
69
height: 480
70
visible: true
71
color: "red"
72
73
Text {
74
anchors.centerIn: parent
75
text: "Hello, World!"
76
}
77
}
78
\endqml
79
80
If we save that document as "HelloWorld.qml", we can load and display it.
81
82
\section1 Creating and Running QML Projects
83
84
To display the graphical scene defined by the QML document, it may be loaded
85
with \l{\QC Documentation}{\QC}. To create a new QML project in \QC:
86
\list 1
87
\li Select \uicontrol{File} > \uicontrol{New Project} > \uicontrol{Qt Quick Application}
88
from within \QC.
89
\li Enter a name for your project and select a location to save it.
90
\li Select the appropriate Qt version and optionally configure version
91
control settings for the project.
92
\li Review the summary of your project settings and continue to complete the project build.
93
\endlist
94
95
When finished, \QC will generate the necessary files and open the project for development.
96
Pressing the green \gui{Run} button runs the application. You should see the
97
text \gui{Hello, World!} in the center of a red rectangle.
98
99
For more information about creating and running projects in \QC, visit
100
the following pages:
101
\list
102
\li \l{\QC: Create Qt Quick Applications}
103
\li \l{\QC: Tutorial: Build and run}
104
\endlist
105
106
\section1 Creating QML Applications with Controls
107
108
While Qt Quick provides basic graphical elements, \l{Qt Quick Controls} provides
109
ready-made QML types for use within an application.
110
111
Inserting the \l[QtQuickControls2]{ApplicationWindow} type is a good starting
112
point for creating applications. An application UI has this basic layout:
113
114
\image applicationwindow.png
115
{ApplicationWindow layout with menu bar, tool bar, content area,
116
and status bar}
117
118
Within each area, different \e controls may be added and connected to form
119
an application. For example, the following snippet is a basic application that
120
demonstrates the use of available space:
121
122
\qml
123
//import related modules
124
import QtQuick
125
import QtQuick.Controls
126
127
//window containing the application
128
ApplicationWindow {
129
width: 640
130
height: 480
131
visible: true
132
//title of the application
133
title: qsTr("Hello World")
134
135
//menu containing two menu items
136
header: MenuBar {
137
Menu {
138
title: qsTr("&File")
139
Action {
140
text: qsTr("&Open...")
141
onTriggered: console.log("Open action triggered")
142
}
143
MenuSeparator { }
144
Action {
145
text: qsTr("&Exit")
146
onTriggered: Qt.quit()
147
}
148
}
149
}
150
151
//Content Area
152
153
//a button in the middle of the content area
154
Button {
155
text: qsTr("Hello World")
156
anchors.horizontalCenter: parent.horizontalCenter
157
anchors.verticalCenter: parent.verticalCenter
158
}
159
}
160
\endqml
161
162
The application has two menu items and a button in the middle. Clicking on the
163
\uicontrol Exit menu item closes the application.
164
165
There are also different navigation methods and different controls such as
166
buttons and sliders. The following examples are available from
167
\QC and demonstrate different controls and layouts.
168
169
\list
170
\li \l{Qt Quick Layouts - Basic Example}{Basic Layouts}
171
\li \l{Qt Quick Controls - Gallery}
172
\endlist
173
174
Feel free to copy and paste the snippets onto this simple Hello World
175
application to see how QML works.
176
177
\section1 Handling User Input
178
179
One of the great advantages of using QML to define a user interface is that it
180
allows the user interface designer to define how the application should react
181
to events with simple JavaScript expressions. In QML, we refer to those events
182
as \l{Signal and Handler Event System}{signals} and these signals are handled by
183
\l{qml-signals-and-handlers}{signal handlers}.
184
185
For example, consider the following example:
186
\qml
187
import QtQuick
188
189
Window {
190
id: root
191
width: 200
192
height: 100
193
color: isRed ? "red" : "blue"
194
visible: true
195
196
property bool isRed: true // Track the color state
197
198
Text {
199
anchors.centerIn: parent
200
text: "Hello, World!"
201
}
202
203
TapHandler {
204
onTapped: root.isRed = !root.isRed // Toggle state
205
}
206
}
207
\endqml
208
209
This example can be saved as "ClickableHelloWorld.qml" and run with \c qml, the
210
\l{qml_runtime_tool}{QML Runtime Tool}.
211
Whenever the user clicks anywhere in the window, the rectangle will change
212
from red to blue. Tapping again will change it back to red.
213
214
\note \l TapHandler also emits the tapped signal for touch events, so this
215
code will also work on a mobile device.
216
217
Keyboard user input can be similarly handled with a simple expression:
218
219
\qml
220
import QtQuick
221
222
Window {
223
id: root
224
width: 200
225
height: 100
226
color: "red"
227
visible: true
228
229
Text {
230
id: myText
231
anchors.centerIn: parent
232
text: toggle ? "Hello, World!" : "Goodbye, World!"
233
focus: true
234
property bool toggle: true
235
Keys.onReturnPressed: (event)=>{
236
myText.toggle = !myText.toggle;
237
event.accepted = true;
238
}
239
}
240
}
241
\endqml
242
243
Now, each time you press the Enter key, the text will alternate between
244
"Hello, World" and "Goodbye, World".
245
246
\section1 Property Bindings
247
248
Objects and their properties form the basis of a graphical interface defined
249
in a QML document. The QML language allows properties to be bound to each
250
other in various ways, enabling highly dynamic user interfaces.
251
252
In the following example, the geometry of each child \l Rectangle is bound to
253
that of the parent \l Window. If the geometry of the parent \l Window object
254
were to change, the geometry of each child \l Rectangle would automatically
255
update due to the property bindings.
256
257
\qml
258
import QtQuick
259
260
Window {
261
id: root
262
width: 200
263
height: 100
264
color: "red"
265
visible: true
266
267
268
Rectangle {
269
width: root.width / 2
270
height: root.height
271
color: "blue"
272
}
273
274
Rectangle {
275
width: root.width / 2
276
height: root.height
277
x: root.width / 2
278
color: "green"
279
}
280
}
281
\endqml
282
283
\section1 Animations
284
285
Properties can also be dynamically updated via animations. The \c QtQuick
286
import provides various animation types which can be used to animate changes
287
to a property's value. In the following example, a property is animated which
288
then gets displayed in a \l Text area:
289
290
\qml
291
import QtQuick
292
293
Window {
294
id: root
295
width: 200
296
height: 100
297
color: "red"
298
visible: true
299
300
property int animatedValue
301
302
SequentialAnimation on animatedValue {
303
loops: Animation.Infinite
304
PropertyAnimation {
305
to: 150
306
duration: 1000
307
}
308
PropertyAnimation {
309
to: 0
310
duration: 1000
311
}
312
}
313
314
Text {
315
anchors.centerIn: parent
316
text: root.animatedValue
317
}
318
}
319
\endqml
320
321
The value being displayed will vary from 0 to 150 periodically.
322
323
324
\section1 Defining Custom QML Types for Re-use
325
326
One of the most important concepts in QML is that of type re-use. An
327
application will probably have multiple visual types which are all similar
328
(for example, multiple push buttons), and QML allows these sort of things to
329
be defined as re-usable, custom types, to minimize code duplication and
330
maximize readability.
331
332
For example, imagine that the developer defines a new \c MessageLabel type in the
333
\c MessageLabel.qml file:
334
335
\snippet qmlapp/qml-extending-types/components/MessageLabel.qml 0
336
337
That type may now be re-used multiple times in the application, as follows:
338
339
\table
340
\row
341
\li \snippet qmlapp/qml-extending-types/components/application.qml 0
342
\li \borderedimage qmlapp/qml-extending-types.gif
343
{MessageLabel components showing debug, warning, and critical messages}
344
\endtable
345
346
347
In this way, modular user interface types are assembled and reused within
348
an application.
349
350
See \l {QML Object Attributes}
351
for more details on how to develop your own reusable components.
352
353
\section1 Where to Go from Here
354
355
Now that you have seen QML in action, you are ready to take your next step.
356
The follow page will lead you in your journey with QML.
357
358
\list
359
\li \l{QML Applications}
360
\li \l{Qt Quick Examples and Tutorials}
361
\endlist
362
363
*/
qtdeclarative
src
quick
doc
src
getting-started
firststepsqml.qdoc
Generated on
for Qt by
1.16.1