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