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
116
Within each area, different \e controls may be added and connected to form
117
an application. For example, the following snippet is a basic application that
118
demonstrates the use of available space:
119
120
\qml
121
//import related modules
122
import QtQuick
123
import QtQuick.Controls
124
125
//window containing the application
126
ApplicationWindow {
127
width: 640
128
height: 480
129
visible: true
130
//title of the application
131
title: qsTr("Hello World")
132
133
//menu containing two menu items
134
header: MenuBar {
135
Menu {
136
title: qsTr("&File")
137
Action {
138
text: qsTr("&Open...")
139
onTriggered: console.log("Open action triggered")
140
}
141
MenuSeparator { }
142
Action {
143
text: qsTr("&Exit")
144
onTriggered: Qt.quit()
145
}
146
}
147
}
148
149
//Content Area
150
151
//a button in the middle of the content area
152
Button {
153
text: qsTr("Hello World")
154
anchors.horizontalCenter: parent.horizontalCenter
155
anchors.verticalCenter: parent.verticalCenter
156
}
157
}
158
\endqml
159
160
The application has two menu items and a button in the middle. Clicking on the
161
\uicontrol Exit menu item closes the application.
162
163
There are also different navigation methods and different controls such as
164
buttons and sliders. The following examples are available from
165
\QC and demonstrate different controls and layouts.
166
167
\list
168
\li \l{Qt Quick Layouts - Basic Example}{Basic Layouts}
169
\li \l{Qt Quick Controls - Gallery}
170
\endlist
171
172
Feel free to copy and paste the snippets onto this simple Hello World
173
application to see how QML works.
174
175
\section1 Handling User Input
176
177
One of the great advantages of using QML to define a user interface is that it
178
allows the user interface designer to define how the application should react
179
to events with simple JavaScript expressions. In QML, we refer to those events
180
as \l{Signal and Handler Event System}{signals} and these signals are handled by
181
\l{qml-signals-and-handlers}{signal handlers}.
182
183
For example, consider the following example:
184
\qml
185
import QtQuick
186
187
Window {
188
id: root
189
width: 200
190
height: 100
191
color: isRed ? "red" : "blue"
192
visible: true
193
194
property bool isRed: true // Track the color state
195
196
Text {
197
anchors.centerIn: parent
198
text: "Hello, World!"
199
}
200
201
TapHandler {
202
onTapped: root.isRed = !root.isRed // Toggle state
203
}
204
}
205
\endqml
206
207
This example can be saved as "ClickableHelloWorld.qml" and run with \c qml, the
208
\l{qml_runtime_tool}{QML Runtime Tool}.
209
Whenever the user clicks anywhere in the window, the rectangle will change
210
from red to blue. Tapping again will change it back to red.
211
212
\note \l TapHandler also emits the tapped signal for touch events, so this
213
code will also work on a mobile device.
214
215
Keyboard user input can be similarly handled with a simple expression:
216
217
\qml
218
import QtQuick
219
220
Window {
221
id: root
222
width: 200
223
height: 100
224
color: "red"
225
visible: true
226
227
Text {
228
id: myText
229
anchors.centerIn: parent
230
text: toggle ? "Hello, World!" : "Goodbye, World!"
231
focus: true
232
property bool toggle: true
233
Keys.onReturnPressed: (event)=>{
234
myText.toggle = !myText.toggle;
235
event.accepted = true;
236
}
237
}
238
}
239
\endqml
240
241
Now, each time you press the Enter key, the text will alternate between
242
"Hello, World" and "Goodbye, World".
243
244
\section1 Property Bindings
245
246
Objects and their properties form the basis of a graphical interface defined
247
in a QML document. The QML language allows properties to be bound to each
248
other in various ways, enabling highly dynamic user interfaces.
249
250
In the following example, the geometry of each child \l Rectangle is bound to
251
that of the parent \l Window. If the geometry of the parent \l Window object
252
were to change, the geometry of each child \l Rectangle would automatically
253
update due to the property bindings.
254
255
\qml
256
import QtQuick
257
258
Window {
259
id: root
260
width: 200
261
height: 100
262
color: "red"
263
visible: true
264
265
266
Rectangle {
267
width: root.width / 2
268
height: root.height
269
color: "blue"
270
}
271
272
Rectangle {
273
width: root.width / 2
274
height: root.height
275
x: root.width / 2
276
color: "green"
277
}
278
}
279
\endqml
280
281
\section1 Animations
282
283
Properties can also be dynamically updated via animations. The \c QtQuick
284
import provides various animation types which can be used to animate changes
285
to a property's value. In the following example, a property is animated which
286
then gets displayed in a \l Text area:
287
288
\qml
289
import QtQuick
290
291
Window {
292
id: root
293
width: 200
294
height: 100
295
color: "red"
296
visible: true
297
298
property int animatedValue
299
300
SequentialAnimation on animatedValue {
301
loops: Animation.Infinite
302
PropertyAnimation {
303
to: 150
304
duration: 1000
305
}
306
PropertyAnimation {
307
to: 0
308
duration: 1000
309
}
310
}
311
312
Text {
313
anchors.centerIn: parent
314
text: root.animatedValue
315
}
316
}
317
\endqml
318
319
The value being displayed will vary from 0 to 150 periodically.
320
321
322
\section1 Defining Custom QML Types for Re-use
323
324
One of the most important concepts in QML is that of type re-use. An
325
application will probably have multiple visual types which are all similar
326
(for example, multiple push buttons), and QML allows these sort of things to
327
be defined as re-usable, custom types, to minimize code duplication and
328
maximize readability.
329
330
For example, imagine that the developer defines a new \c MessageLabel type in the
331
\c MessageLabel.qml file:
332
333
\snippet qmlapp/qml-extending-types/components/MessageLabel.qml 0
334
335
That type may now be re-used multiple times in the application, as follows:
336
337
\table
338
\row
339
\li \snippet qmlapp/qml-extending-types/components/application.qml 0
340
\li \borderedimage qmlapp/qml-extending-types.gif
341
\endtable
342
343
344
In this way, modular user interface types are assembled and reused within
345
an application.
346
347
See \l {QML Object Attributes}
348
for more details on how to develop your own reusable components.
349
350
\section1 Where to Go from Here
351
352
Now that you have seen QML in action, you are ready to take your next step.
353
The follow page will lead you in your journey with QML.
354
355
\list
356
\li \l{QML Applications}
357
\li \l{Qt Quick Examples and Tutorials}
358
\endlist
359
360
*/
qtdeclarative
src
quick
doc
src
getting-started
firststepsqml.qdoc
Generated on
for Qt by
1.16.1