Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
basics.qdoc
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3/*!
4\page qtqml-syntax-basics.html
5\title QML Syntax Basics
6\brief Description of the basics of QML syntax
7
8QML is a multi-paradigm language that enables objects to be defined in terms of their attributes
9and how they relate and respond to changes in other objects. In contrast to purely imperative code,
10where changes in attributes and behavior are expressed through a series of statements that are
11processed step by step, QML's declarative syntax integrates attribute and behavioral changes
12directly into the definitions of individual objects. These attribute definitions can then include
13imperative code, in the case where complex custom application behavior is needed.
14
15QML source code is generally loaded by the engine through QML \e documents, which are
16standalone documents of QML code. These can be used to define \l {QML Object Types}{QML object types} that can then be reused throughout an application.
17Note that type names must begin with an uppercase letter in order
18to be declared as QML object types in a QML file.
19
20
21\section1 Import Statements
22
23A QML document may have one or more imports at the top of the file.
24An import can be any one of:
25
26\list
27\li a versioned namespace into which types have been registered (e.g., by a plugin)
28\li a relative directory which contains type-definitions as QML documents
29\li a JavaScript file
30\endlist
31
32JavaScript file imports must be qualified when imported, so that the properties and methods they provide can be accessed.
33
34The generic form of the various imports are as follows:
35\list
36\li \tt{import Namespace VersionMajor.VersionMinor}
37\li \tt{import Namespace VersionMajor.VersionMinor as SingletonTypeIdentifier}
38\li \tt{import "directory"}
39\li \tt{import "file.js" as ScriptIdentifier}
40\endlist
41
42Examples:
43\list
44\li \tt{import QtQuick 2.0}
45\li \tt{import QtQuick.LocalStorage 2.0 as Database}
46\li \tt{import "../privateComponents"}
47\li \tt{import "somefile.js" as Script}
48\endlist
49
50Please see the \l{qtqml-syntax-imports.html}{QML Syntax - Import Statements}
51documentation for in-depth information about QML imports.
52
53
54\target qml-object-declarations
55\section1 Object Declarations
56
57Syntactically, a block of QML code defines a tree of QML objects to be created. Objects are
58defined using \e {object declarations} that describe the type of object to be created as well
59as the attributes that are to be given to the object. Each object may also declare child objects
60using nested object declarations.
61
62An object declaration consists of the name of its object type, followed by a set of curly braces. All attributes and child objects are then declared within these braces.
63
64Here is a simple object declaration:
65
66\qml
67Rectangle {
68 width: 100
69 height: 100
70 color: "red"
71}
72\endqml
73
74This declares an object of type \l Rectangle, followed by a set of curly braces that encompasses the attributes defined for that object. The \l Rectangle type is a type made available by the \c QtQuick module, and the attributes defined in this case are the values of the rectangle's \c width, \c height and \c color properties. (These are properties made available by the \l Rectangle type, as described in the \l Rectangle documentation.)
75
76The above object can be loaded by the engine if it is part of a \l{qtqml-documents-topic.html}{QML document}. That is, if the source code is complemented with \e import statement that imports the \c QtQuick module (to make the \l Rectangle type available), as below:
77
78\qml
79import QtQuick 2.0
80
81Rectangle {
82 width: 100
83 height: 100
84 color: "red"
85}
86\endqml
87
88When placed into a \c .qml file and loaded by the QML engine, the above code creates a \l Rectangle object using the \l Rectangle type supplied by the \c QtQuick module:
89
90\image qtqml-syntax-basics-object-declaration.png
91
92\note If an object definition only has a small number of properties, it can be written on a single line like this, with the properties separated by semi-colons:
93
94\qml
95Rectangle { width: 100; height: 100; color: "red" }
96\endqml
97
98Obviously, the \l Rectangle object declared in this example is very simple indeed, as it defines nothing more than a few property values. To create more useful objects, an object declaration may define many other types of attributes: these are discussed in the \l{qtqml-syntax-objectattributes.html}{QML Object Attributes} documentation. Additionally, an object declaration may define child objects, as discussed below.
99
100
101\section2 Child Objects
102
103Any object declaration can define child objects through nested object declarations. In this way, \b {any object declaration implicitly declares an object tree that may contain any number of child objects}.
104
105For example, the \l Rectangle object declaration below includes a \l Gradient object declaration,
106which in turn contains two \l GradientStop declarations:
107
108\qml
109import QtQuick 2.0
110
111Rectangle {
112 width: 100
113 height: 100
114
115 gradient: Gradient {
116 GradientStop { position: 0.0; color: "yellow" }
117 GradientStop { position: 1.0; color: "green" }
118 }
119}
120\endqml
121
122When this code is loaded by the engine, it creates an object tree with a \l Rectangle object at the root; this object has a \l Gradient child object, which in turn has two \l GradientStop children.
123
124Note, however, that this is a parent-child relationship in the context of the QML object tree, not
125in the context of the visual scene. The concept of a parent-child relationship in a visual scene is provided by the \l Item type from the \c QtQuick module, which is the base type for most QML types, as most QML objects are intended to be visually rendered. For example, \l Rectangle and \l Text are both \l {Item}-based types, and below, a \l Text object has been declared as a visual child of a \l Rectangle object:
126
127\qml
128import QtQuick 2.0
129
130Rectangle {
131 width: 200
132 height: 200
133 color: "red"
134
135 Text {
136 anchors.centerIn: parent
137 text: "Hello, QML!"
138 }
139}
140\endqml
141
142When the \l Text object refers to its \l {Item::parent}{parent} value in the above code, it is referring to its \e {visual parent}, not the parent in the object tree. In this case, they are one and the same: the \l Rectangle object is the parent of the \l Text object in both the context of the QML object tree as well as the context of the visual scene. However, while the \l {Item::parent}{parent} property can be modified to change the visual parent, the parent of an object in the context of the object tree cannot be changed from QML.
143
144(Additionally, notice that the \l Text object has been declared without assigning it to a property of the \l Rectangle, unlike the earlier example which assigned a \l Gradient object to the rectangle's \c gradient property. This is because the \l {Item::children}{children} property of \l Item has been set as the type's \l {qtqml-syntax-objectattributes.html#default-properties}{default property} to enable this more convenient syntax.)
145
146See the \l{qtquick-visualcanvas-visualparent.html}{visual parent} documentation for more information on the concept of visual parenting with the \l Item type.
147
148
149\section1 Comments
150
151The syntax for commenting in QML is similar to that of JavaScript:
152
153\list
154\li Single line comments start with // and finish at the end of the line.
155\li Multiline comments start with /* and finish with *\/
156\endlist
157
158\snippet qml/comments.qml 0
159
160Comments are ignored by the engine when processing QML code. They are useful for explaining what a section of code is doing, whether for reference at a later date or for explaining the implementation to others.
161
162Comments can also be used to prevent the execution of code, which is sometimes useful for tracking down problems.
163
164\qml
165 Text {
166 text: "Hello world!"
167 //opacity: 0.5
168 }
169\endqml
170
171In the above example, the \l Text object will have normal opacity, since the line opacity: 0.5 has been turned into a comment.
172*/