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