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
116Within each area, different \e controls may be added and connected to form
117an application. For example, the following snippet is a basic application that
118demonstrates the use of available space:
119
120\qml
121//import related modules
122import QtQuick
123import QtQuick.Controls
124
125//window containing the application
126ApplicationWindow {
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
160The application has two menu items and a button in the middle. Clicking on the
161\uicontrol Exit menu item closes the application.
162
163There are also different navigation methods and different controls such as
164buttons 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
172Feel free to copy and paste the snippets onto this simple Hello World
173application to see how QML works.
174
175\section1 Handling User Input
176
177One of the great advantages of using QML to define a user interface is that it
178allows the user interface designer to define how the application should react
179to events with simple JavaScript expressions. In QML, we refer to those events
180as \l{Signal and Handler Event System}{signals} and these signals are handled by
181\l{qml-signals-and-handlers}{signal handlers}.
182
183For example, consider the following example:
184\qml
185import QtQuick
186
187Window {
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
207This example can be saved as "ClickableHelloWorld.qml" and run with \c qml, the
208\l{qml_runtime_tool}{QML Runtime Tool}.
209Whenever the user clicks anywhere in the window, the rectangle will change
210from 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
213code will also work on a mobile device.
214
215Keyboard user input can be similarly handled with a simple expression:
216
217\qml
218import QtQuick
219
220Window {
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
241Now, each time you press the Enter key, the text will alternate between
242"Hello, World" and "Goodbye, World".
243
244\section1 Property Bindings
245
246Objects and their properties form the basis of a graphical interface defined
247in a QML document. The QML language allows properties to be bound to each
248other in various ways, enabling highly dynamic user interfaces.
249
250In the following example, the geometry of each child \l Rectangle is bound to
251that of the parent \l Window. If the geometry of the parent \l Window object
252were to change, the geometry of each child \l Rectangle would automatically
253update due to the property bindings.
254
255\qml
256import QtQuick
257
258Window {
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
283Properties can also be dynamically updated via animations. The \c QtQuick
284import provides various animation types which can be used to animate changes
285to a property's value. In the following example, a property is animated which
286then gets displayed in a \l Text area:
287
288\qml
289import QtQuick
290
291Window {
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
319The value being displayed will vary from 0 to 150 periodically.
320
321
322\section1 Defining Custom QML Types for Re-use
323
324One of the most important concepts in QML is that of type re-use. An
325application will probably have multiple visual types which are all similar
326(for example, multiple push buttons), and QML allows these sort of things to
327be defined as re-usable, custom types, to minimize code duplication and
328maximize readability.
329
330For 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
335That 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
344In this way, modular user interface types are assembled and reused within
345an application.
346
347See \l {QML Object Attributes}
348for more details on how to develop your own reusable components.
349
350\section1 Where to Go from Here
351
352Now that you have seen QML in action, you are ready to take your next step.
353The 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*/