source: trunk/doc/src/declarative/qdeclarativeintro.qdoc@ 980

Last change on this file since 980 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 10.3 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the documentation of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:FDL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in a
14** written agreement between you and Nokia.
15**
16** GNU Free Documentation License
17** Alternatively, this file may be used under the terms of the GNU Free
18** Documentation License version 1.3 as published by the Free Software
19** Foundation and appearing in the file included in the packaging of this
20** file.
21**
22** If you have questions regarding the use of this file, please contact
23** Nokia at [email protected].
24** $QT_END_LICENSE$
25**
26****************************************************************************/
27
28/*!
29\page qdeclarativeintroduction.html
30\title Introduction to the QML language
31
32\tableofcontents
33
34QML is a declarative language designed to describe the user interface of a
35program: both what it looks like, and how it behaves. In QML, a user
36interface is specified as a tree of objects with properties.
37
38This introduction is meant for those with little or no programming
39experience. JavaScript is used as a scripting language in QML, so you may want
40to learn a bit more about it (see the \l{Javascript Guide}) before diving
41deeper into QML. It's also helpful to have a basic understanding of other web
42technologies like HTML and CSS, but it's not required.
43
44\section1 Basic QML Syntax
45
46QML looks like this:
47
48\code
49import QtQuick 1.0
50
51Rectangle {
52 width: 200
53 height: 200
54 color: "blue"
55
56 Image {
57 source: "pics/logo.png"
58 anchors.centerIn: parent
59 }
60}
61\endcode
62
63Here we create two objects, a \l Rectangle object and its child
64\l Image object. Objects are specified by their type, followed by a pair of
65braces in between which additional data can be defined for the object, such as
66its property values and any child objects.
67
68Properties are specified with a \c {property: value} syntax. In the above example, we
69can see the \l Image object has a property named \c source, which has been assigned the
70value \c "pics/logo.png". The property and its value are separated by a colon.
71
72Properties can be specified one-per-line:
73
74\code
75Rectangle {
76 width: 100
77 height: 100
78}
79\endcode
80
81or you can put multiple properties on a single line:
82
83\code
84Rectangle { width: 100; height: 100 }
85\endcode
86
87When multiple property/value pairs are specified on a single line, they
88must be separated by a semicolon.
89
90The \c import statement imports the \c QtQuick \l{QML Modules}{module}, which contains all of the
91standard \l {QML Elements}. Without this import statement, the \l Rectangle
92and \l Image elements would not be available.
93
94
95
96\section1 Comments
97
98Commenting in QML is similar to JavaScript.
99\list
100\o Single line comments start with // and finish at the end of the line.
101\o Multiline comments start with /* and finish with *\/
102\endlist
103
104\snippet doc/src/snippets/declarative/comments.qml 0
105
106Comments are ignored by the engine. They are useful for explaining what you
107are doing; for referring back to at a later date, or for others reading
108your QML files.
109
110Comments can also be used to prevent the execution of code, which is
111sometimes useful for tracking down problems.
112
113\code
114Text {
115 text: "Hello world!"
116 //opacity: 0.5
117}
118\endcode
119
120In the above example, the \l Text object will have normal opacity, since the
121line opacity: 0.5 has been turned into a comment.
122
123
124
125\section1 Object identifiers
126
127Each object can be given a special \e id value that allows the object to be identified
128and referred to by other objects.
129
130For example, below we have two \l Text objects. The first \l Text object
131has an \c id value of "text1". The second \l Text object can now set its own
132\c text property value to be the same as that of the first object, by referring to
133\c text1.text:
134
135\qml
136import QtQuick 1.0
137
138Row {
139 Text {
140 id: text1
141 text: "Hello World"
142 }
143
144 Text { text: text1.text }
145}
146\endqml
147
148An object can be referred to by its \c id from anywhere within the \l {QML Documents}{component}
149in which it is declared. Therefore, an \c id value must always be unique within a single component.
150
151The \c id value is a special value for a QML object and should not be thought of as an
152ordinary object property; for example, it is not possible to access \c text1.id in the
153above example. Once an object is created, its \c id cannot be changed.
154
155Note that an \c id must begin with a lower-case letter or an underscore, and cannot contain
156characters other than letters, numbers and underscores.
157
158
159
160\section1 Expressions
161
162JavaScript expressions can be used to assign property values. For example:
163
164\code
165Item {
166 width: 100 * 3
167 height: 50 + 22
168}
169\endcode
170
171These expressions can include references to other objects and properties, in which case
172a \l{Property Binding}{binding} is established: when the value of the expression changes,
173the property to which the expression is assigned is automatically updated to the
174new value. For example:
175
176\code
177Item {
178 width: 300
179 height: 300
180
181 Rectangle {
182 width: parent.width - 50
183 height: 100
184 color: "yellow"
185 }
186}
187\endcode
188
189Here, the \l Rectangle object's \c width property is set relative to the width
190of its parent. Whenever the parent's width changes, the width of the \l Rectangle is
191automatically updated.
192
193
194
195\section1 Properties
196\target intro-properties
197
198\section2 Basic property types
199
200QML supports properties of many types (see \l{QML Basic Types}). The basic types include \c int,
201\c real, \c bool, \c string and \c color.
202
203\code
204Item {
205 x: 10.5 // a 'real' property
206 state: "details" // a 'string' property
207 focus: true // a 'bool' property
208 ...
209}
210\endcode
211
212QML properties are what is known as \e type-safe. That is, they only allow you to assign a value that
213matches the property type. For example, the \c x property of item is a real, and if you try to assign
214a string to it you will get an error.
215
216\badcode
217Item {
218 x: "hello" // illegal!
219}
220\endcode
221
222Note that with the exception of \l {Attached Properties}, properties always begin with a lowercase
223letter.
224
225
226\section2 Property change notifications
227
228When a property changes value, it can send a signal to notify others of this change.
229
230To receive these signals, simply create a \e {signal handler} named with an \c on<Property>Changed
231syntax. For example, the \l Rectangle element has \l {Item::}{width} and \l {Rectangle::}{color}
232properties. Below, we have a \l Rectangle object that has defined two signal handlers,
233\c onWidthChanged and \c onColorChanged, which will automaticallly be called whenever these
234properties are modified:
235
236\qml
237Rectangle {
238 width: 100; height: 100
239
240 onWidthChanged: console.log("Width has changed to:", width)
241 onColorChanged: console.log("Color has changed to:", color)
242}
243\endqml
244
245Signal handlers are explained further \l {Signal Handlers}{below}.
246
247
248\section2 List properties
249
250List properties look like this:
251
252\code
253Item {
254 children: [
255 Image {},
256 Text {}
257 ]
258}
259\endcode
260
261The list is enclosed in square brackets, with a comma separating the
262list elements. In cases where you are only assigning a single item to a
263list, you can omit the square brackets:
264
265\code
266Image {
267 children: Rectangle {}
268}
269\endcode
270
271Items in the list can be accessed by index. See the \l{list}{list type} documentation
272for more details about list properties and their available operations.
273
274
275\section2 Default properties
276
277Each object type can specify one of its list or object properties as its default property.
278If a property has been declared as the default property, the property tag can be omitted.
279
280For example this code:
281\code
282State {
283 changes: [
284 PropertyChanges {},
285 PropertyChanges {}
286 ]
287}
288\endcode
289
290can be simplified to:
291
292\code
293State {
294 PropertyChanges {}
295 PropertyChanges {}
296}
297\endcode
298
299because \c changes is the default property of the \c State type.
300
301\section2 Grouped Properties
302\target dot properties
303
304In some cases properties form a logical group and use a 'dot' or grouped notation
305to show this.
306
307Grouped properties can be written like this:
308\qml
309Text {
310 font.pixelSize: 12
311 font.bold: true
312}
313\endqml
314
315or like this:
316\qml
317Text {
318 font { pixelSize: 12; bold: true }
319}
320\endqml
321
322In the element documentation grouped properties are shown using the 'dot' notation.
323
324\section2 Attached Properties
325\target attached-properties
326
327Some objects attach properties to another object. Attached Properties
328are of the form \e {Type.property} where \e Type is the type of the
329element that attaches \e property.
330
331For example, the \l ListView element attaches the \e ListView.isCurrentItem property
332to each delegate it creates:
333
334\code
335Component {
336 id: myDelegate
337 Text {
338 text: "Hello"
339 color: ListView.isCurrentItem ? "red" : "blue"
340 }
341}
342ListView {
343 delegate: myDelegate
344}
345\endcode
346
347Another example of attached properties is the \l Keys element which
348attaches properties for handling key presses to
349any visual Item, for example:
350
351\code
352Item {
353 focus: true
354 Keys.onSelectPressed: console.log("Selected")
355}
356\endcode
357
358\section1 Signal Handlers
359
360Signal handlers allow JavaScript code to be executed in response to an event. For
361example, the \l MouseArea element has an \l {MouseArea::}{onClicked} handler that can
362be used to respond to a mouse click. Below, we use this handler to print a
363message whenever the mouse is clicked:
364
365\code
366Item {
367 width: 100; height: 100
368
369 MouseArea {
370 anchors.fill: parent
371 onClicked: {
372 console.log("mouse button clicked")
373 }
374 }
375}
376\endcode
377
378All signal handlers begin with \e "on".
379
380Some signal handlers include an optional parameter. For example
381the MouseArea \l{MouseArea::}{onPressed} signal handler has a \c mouse parameter
382that contains information about the mouse press. This parameter can be referred to in
383the JavaScript code, as below:
384
385\code
386MouseArea {
387 acceptedButtons: Qt.LeftButton | Qt.RightButton
388 onPressed: {
389 if (mouse.button == Qt.RightButton)
390 console.log("Right mouse button pressed")
391 }
392}
393\endcode
394
395
396*/
Note: See TracBrowser for help on using the repository browser.