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
10A QML document defines a hierarchy of objects with a highly-readable,
11structured layout. Every QML document consists of two parts: an imports
12section and an object declaration section. The types and functionality most
13common to user interfaces are provided in the \c{QtQuick}
14import.
15
16\section2 Importing and Using the QtQuick Module
17
18To use the \l{Qt Quick} module, a QML document needs to
19import it. The import syntax looks like this:
20
21\qml
22import QtQuick
23\endqml
24
25The types and functionality that \l{Qt Quick} provides can now
26be used in the QML document!
27
28\section2 Defining an Object Hierarchy
29
30The object declaration in a QML document defines what will be displayed in the
31visual scene. \l{Qt Quick} provides the basic building blocks
32for all user interfaces, such as the objects for displaying images and text and
33for handling user input.
34
35A simple object declaration might be a colored window with some text centered
36in it:
37
38\qml
39Window {
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
52This defines an object hierarchy with a root \l Window object
53which has a child \l Text object. The \c parent of the \l Text object is
54automatically set to the \l Window, and similarly, the \l Text object is
55added to the \c children property of the \l Window object, by QML.
56
57\section2 Putting it All Together
58
59The \l Window and \l Text types used in the above example are both provided
60by the \c{QtQuick} import. Putting the import and object declaration
61together, we get a complete QML document:
62
63\qml
64import QtQuick
65
66Window {
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
79If we save that document as "HelloWorld.qml", we can load and display it.
80
81\section1 Creating and Running QML Projects
82
83To display the graphical scene defined by the QML document, it may be loaded
84with \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}
87from 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
90control settings for the project.
91\li Review the summary of your project settings and continue to complete the project build.
92\endlist
93
94When finished, \QC will generate the necessary files and open the project for development.
95Pressing the green \gui{Run} button runs the application. You should see the
96text \gui{Hello, World!} in the center of a red rectangle.
97
98For more information about creating and running projects in \QC, visit
99the 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
107While Qt Quick provides basic graphical elements, \l{Qt Quick Controls} provides
108ready-made QML types for use within an application.
109
110Inserting the \l[QtQuickControls2]{ApplicationWindow} type is a good starting
111point for creating applications. An application UI has this basic layout:
112
113\image applicationwindow.png
114
115Within each area, different \e controls may be added and connected to form
116an application. For example, the following snippet is a basic application that
117demonstrates the use of available space:
118
119\qml
120//import related modules
121import QtQuick
122import QtQuick.Controls
123
124//window containing the application
125ApplicationWindow {
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
159The application has two menu items and a button in the middle. Clicking on the
160\uicontrol Exit menu item closes the application.
161
162There are also different navigation methods and different controls such as
163buttons 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
171Feel free to copy and paste the snippets onto this simple Hello World
172application to see how QML works.
173
174\section1 Handling User Input
175
176One of the great advantages of using QML to define a user interface is that it
177allows the user interface designer to define how the application should react
178to events with simple JavaScript expressions. In QML, we refer to those events
179as \l{Signal and Handler Event System}{signals} and these signals are handled by
180\l{qml-signals-and-handlers}{signal handlers}.
181
182For example, consider the following example:
183\qml
184import QtQuick
185
186Window {
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
206This example can be saved as "ClickableHelloWorld.qml" and run with \c qml, the
207\l{qml_runtime_tool}{QML Runtime Tool}.
208Whenever the user clicks anywhere in the window, the rectangle will change
209from 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
212code will also work on a mobile device.
213
214Keyboard user input can be similarly handled with a simple expression:
215
216\qml
217import QtQuick
218
219Window {
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
240Now, each time you press the Enter key, the text will alternate between
241"Hello, World" and "Goodbye, World".
242
243\section1 Property Bindings
244
245Objects and their properties form the basis of a graphical interface defined
246in a QML document. The QML language allows properties to be bound to each
247other in various ways, enabling highly dynamic user interfaces.
248
249In the following example, the geometry of each child \l Rectangle is bound to
250that of the parent \l Window. If the geometry of the parent \l Window object
251were to change, the geometry of each child \l Rectangle would automatically
252update due to the property bindings.
253
254\qml
255import QtQuick
256
257Window {
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
282Properties can also be dynamically updated via animations. The \c QtQuick
283import provides various animation types which can be used to animate changes
284to a property's value. In the following example, a property is animated which
285then gets displayed in a \l Text area:
286
287\qml
288import QtQuick
289
290Window {
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
318The value being displayed will vary from 0 to 150 periodically.
319
320
321\section1 Defining Custom QML Types for Re-use
322
323One of the most important concepts in QML is that of type re-use. An
324application will probably have multiple visual types which are all similar
325(for example, multiple push buttons), and QML allows these sort of things to
326be defined as re-usable, custom types, to minimize code duplication and
327maximize readability.
328
329For 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
334That 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
343In this way, modular user interface types are assembled and reused within
344an application.
345
346See \l {QML Object Attributes}
347for more details on how to develop your own reusable components.
348
349\section1 Where to Go from Here
350
351Now that you have seen QML in action, you are ready to take your next step.
352The 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*/