summaryrefslogtreecommitdiffstats
path: root/doc/src/declarative/qmlintro.qdoc
blob: cfefdb9e201e66928a483fd7fb6f32c17beaffd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*!
\page qmlintroduction.html
\title Introduction to the QML language

\tableofcontents

\section1 What is QML?

QML is a declarative language designed to describe the user interface of a
program: both what it looks like and how it behaves. In QML, a user
interface is specified as a tree of objects with properties.

\section1 What should I know before starting?

This introduction is meant for those with little or no programming
experience. JavaScript is used as a scripting language in QML, so you may want
to learn a bit more about it (\l{JavaScript: The Definitive Guide}) before diving
too deep into QML. It's also helpful to have a basic understanding of other web
technologies like HTML and CSS, but not required.

\section1 Basic QML Syntax

QML looks like this:

\code
Rectangle {
    width: 200
    height: 200
    color: "white"
    Image {
        source: "pics/logo.png"
        anchors.centerIn: parent
    }
}
\endcode

Objects are specified by their type, followed by a pair of braces. Object
types always begin with a capital letter. In the above example, there are
two objects, a \l Rectangle, and an \l Image. Between the braces, we can specify
information about the object, such as its properties.

Properties are specified as \c {property: value} (much like CSS). In the above
example, we can see the Image has a property named \c source, which has been
assigned the value \c "pics/logo.png". The property and its value are
separated by a colon.

Properties can be specified one-per-line:

\code
Rectangle {
    width: 100
    height: 100
}
\endcode

or you can put multiple properties on a single line:

\code
Rectangle { width: 100; height: 100 }
\endcode

When multiple property/value pairs are specified on a single line, they
must be separated by a semicolon.

\section1 Expressions

In addition to assigning values to properties, you can also assign
expressions written in JavaScript.

\code
Rotation {
    angle: 360 * 3
}
\endcode

These expressions can include references to other objects and properties, in which case
a \e binding is established: when the value of the expression changes, the property the
expression has been assigned to is automatically updated to that value.

\code
Item {
    Text {
        id: text1
        text: "Hello World"
    }
    Text {
        id: text2
        text: Text1.text
    }
}
\endcode

In the example above, the Text2 object will display the same text as Text1. If Text1 is updated,
Text2 will be updated as well.

Note that to refer to other objects, we use their \e id (more information on the id property can be
found in a following section).

\section1 QML Comments

Commenting in QML is similar to JavaScript.
\list
\o Single line comments begin with // and end at the end of the line.
\o Multiline comments begin with /* and end with *\/
\endlist

\quotefile doc/src/snippets/declarative/comments.qml

Comments are ignored by the engine. The are useful for explaining what you
are doing: for referring back to at a later date, or for others reading
your QML files.

Comments can also be used to prevent the execution of code, which is
sometimes useful for tracking down problems.

\code
Text {
    text: "Hello world!"
    //opacity: 0.5
}
\endcode

In the above example, the Text object will have normal opacity, since the
line opacity: 0.5 has been turned into a comment.

\section1 Properties

\section2 Property naming

Properties begin with a lowercase letter (with the exception of \l{Attached Properties}).

\section2 Property types

QML supports properties of many types (\l{Common QML Types}). The basic types include int,
real, bool, string, color, and lists.

\code
Item {
    x: 10.5             // a 'real' property
    ...
    state: "details"    // a 'string' property
    focus: true         // a 'bool' property
}
\endcode

QML properties are what is known as \e typesafe. That is, they only allow you to assign a value that
matches the property type. For example, the scale property of item is a real, and if you try to assign
a string to it you will get an error.

\badcode
Item {
    scale: "hello"  //illegal!
}
\endcode

\section3 The \c id property

The \c id property is a special property of type \e id. Assigning an id to an object allows you
to refer to it elsewhere.

\code
Item {
    Text {
        id: myName
        text: "..."
    }
    Text {
        text: myName.text
    }
}
\endcode

\c Ids must begin with a lowercase letter.

\section2 List properties

List properties look like this:

\code
Item {
    children: [
        Image {},
        Text {}
    ]
}
\endcode

The list is enclosed in square brackets, with a comma separating the
list elements. In cases where you are only assigning a single item to a
list, you can omit the square brackets:

\code
Image {
    children: Rectangle {}
}
\endcode

\section2 Default properties

Each object type can specify one of its list properties as its default property.
If a list property has been declared as the default property, the property tag can be omitted.

For example this code:
\code
State {
    changes: [
        PropertyChanges {},
        PropertyChanges {}
    ]
}
\endcode

can be simplified to:

\code
State {
    PropertyChanges {}
    PropertyChanges {}
}
\endcode

because \c changes is the default property of the \c State type.

\section2 Dot Properties

\section2 Attached Properties
\target attached-properties

\section2 Signal Handlers

*/