[844] | 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 |
|
---|
| 34 | QML is a declarative language designed to describe the user interface of a
|
---|
| 35 | program: both what it looks like, and how it behaves. In QML, a user
|
---|
| 36 | interface is specified as a tree of objects with properties.
|
---|
| 37 |
|
---|
| 38 | This introduction is meant for those with little or no programming
|
---|
| 39 | experience. JavaScript is used as a scripting language in QML, so you may want
|
---|
| 40 | to learn a bit more about it (see the \l{Javascript Guide}) before diving
|
---|
| 41 | deeper into QML. It's also helpful to have a basic understanding of other web
|
---|
| 42 | technologies like HTML and CSS, but it's not required.
|
---|
| 43 |
|
---|
| 44 | \section1 Basic QML Syntax
|
---|
| 45 |
|
---|
| 46 | QML looks like this:
|
---|
| 47 |
|
---|
| 48 | \code
|
---|
| 49 | import QtQuick 1.0
|
---|
| 50 |
|
---|
| 51 | Rectangle {
|
---|
| 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 |
|
---|
| 63 | Here 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
|
---|
| 65 | braces in between which additional data can be defined for the object, such as
|
---|
| 66 | its property values and any child objects.
|
---|
| 67 |
|
---|
| 68 | Properties are specified with a \c {property: value} syntax. In the above example, we
|
---|
| 69 | can see the \l Image object has a property named \c source, which has been assigned the
|
---|
| 70 | value \c "pics/logo.png". The property and its value are separated by a colon.
|
---|
| 71 |
|
---|
| 72 | Properties can be specified one-per-line:
|
---|
| 73 |
|
---|
| 74 | \code
|
---|
| 75 | Rectangle {
|
---|
| 76 | width: 100
|
---|
| 77 | height: 100
|
---|
| 78 | }
|
---|
| 79 | \endcode
|
---|
| 80 |
|
---|
| 81 | or you can put multiple properties on a single line:
|
---|
| 82 |
|
---|
| 83 | \code
|
---|
| 84 | Rectangle { width: 100; height: 100 }
|
---|
| 85 | \endcode
|
---|
| 86 |
|
---|
| 87 | When multiple property/value pairs are specified on a single line, they
|
---|
| 88 | must be separated by a semicolon.
|
---|
| 89 |
|
---|
| 90 | The \c import statement imports the \c QtQuick \l{QML Modules}{module}, which contains all of the
|
---|
| 91 | standard \l {QML Elements}. Without this import statement, the \l Rectangle
|
---|
| 92 | and \l Image elements would not be available.
|
---|
| 93 |
|
---|
| 94 |
|
---|
| 95 |
|
---|
| 96 | \section1 Comments
|
---|
| 97 |
|
---|
| 98 | Commenting 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 |
|
---|
| 106 | Comments are ignored by the engine. They are useful for explaining what you
|
---|
| 107 | are doing; for referring back to at a later date, or for others reading
|
---|
| 108 | your QML files.
|
---|
| 109 |
|
---|
| 110 | Comments can also be used to prevent the execution of code, which is
|
---|
| 111 | sometimes useful for tracking down problems.
|
---|
| 112 |
|
---|
| 113 | \code
|
---|
| 114 | Text {
|
---|
| 115 | text: "Hello world!"
|
---|
| 116 | //opacity: 0.5
|
---|
| 117 | }
|
---|
| 118 | \endcode
|
---|
| 119 |
|
---|
| 120 | In the above example, the \l Text object will have normal opacity, since the
|
---|
| 121 | line opacity: 0.5 has been turned into a comment.
|
---|
| 122 |
|
---|
| 123 |
|
---|
| 124 |
|
---|
| 125 | \section1 Object identifiers
|
---|
| 126 |
|
---|
| 127 | Each object can be given a special \e id value that allows the object to be identified
|
---|
| 128 | and referred to by other objects.
|
---|
| 129 |
|
---|
| 130 | For example, below we have two \l Text objects. The first \l Text object
|
---|
| 131 | has 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
|
---|
| 136 | import QtQuick 1.0
|
---|
| 137 |
|
---|
| 138 | Row {
|
---|
| 139 | Text {
|
---|
| 140 | id: text1
|
---|
| 141 | text: "Hello World"
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | Text { text: text1.text }
|
---|
| 145 | }
|
---|
| 146 | \endqml
|
---|
| 147 |
|
---|
| 148 | An object can be referred to by its \c id from anywhere within the \l {QML Documents}{component}
|
---|
| 149 | in which it is declared. Therefore, an \c id value must always be unique within a single component.
|
---|
| 150 |
|
---|
| 151 | The \c id value is a special value for a QML object and should not be thought of as an
|
---|
| 152 | ordinary object property; for example, it is not possible to access \c text1.id in the
|
---|
| 153 | above example. Once an object is created, its \c id cannot be changed.
|
---|
| 154 |
|
---|
| 155 | Note that an \c id must begin with a lower-case letter or an underscore, and cannot contain
|
---|
| 156 | characters other than letters, numbers and underscores.
|
---|
| 157 |
|
---|
| 158 |
|
---|
| 159 |
|
---|
| 160 | \section1 Expressions
|
---|
| 161 |
|
---|
| 162 | JavaScript expressions can be used to assign property values. For example:
|
---|
| 163 |
|
---|
| 164 | \code
|
---|
| 165 | Item {
|
---|
| 166 | width: 100 * 3
|
---|
| 167 | height: 50 + 22
|
---|
| 168 | }
|
---|
| 169 | \endcode
|
---|
| 170 |
|
---|
| 171 | These expressions can include references to other objects and properties, in which case
|
---|
| 172 | a \l{Property Binding}{binding} is established: when the value of the expression changes,
|
---|
| 173 | the property to which the expression is assigned is automatically updated to the
|
---|
| 174 | new value. For example:
|
---|
| 175 |
|
---|
| 176 | \code
|
---|
| 177 | Item {
|
---|
| 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 |
|
---|
| 189 | Here, the \l Rectangle object's \c width property is set relative to the width
|
---|
| 190 | of its parent. Whenever the parent's width changes, the width of the \l Rectangle is
|
---|
| 191 | automatically updated.
|
---|
| 192 |
|
---|
| 193 |
|
---|
| 194 |
|
---|
| 195 | \section1 Properties
|
---|
| 196 | \target intro-properties
|
---|
| 197 |
|
---|
| 198 | \section2 Basic property types
|
---|
| 199 |
|
---|
| 200 | QML 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
|
---|
| 204 | Item {
|
---|
| 205 | x: 10.5 // a 'real' property
|
---|
| 206 | state: "details" // a 'string' property
|
---|
| 207 | focus: true // a 'bool' property
|
---|
| 208 | ...
|
---|
| 209 | }
|
---|
| 210 | \endcode
|
---|
| 211 |
|
---|
| 212 | QML properties are what is known as \e type-safe. That is, they only allow you to assign a value that
|
---|
| 213 | matches the property type. For example, the \c x property of item is a real, and if you try to assign
|
---|
| 214 | a string to it you will get an error.
|
---|
| 215 |
|
---|
| 216 | \badcode
|
---|
| 217 | Item {
|
---|
| 218 | x: "hello" // illegal!
|
---|
| 219 | }
|
---|
| 220 | \endcode
|
---|
| 221 |
|
---|
| 222 | Note that with the exception of \l {Attached Properties}, properties always begin with a lowercase
|
---|
| 223 | letter.
|
---|
| 224 |
|
---|
| 225 |
|
---|
| 226 | \section2 Property change notifications
|
---|
| 227 |
|
---|
| 228 | When a property changes value, it can send a signal to notify others of this change.
|
---|
| 229 |
|
---|
| 230 | To receive these signals, simply create a \e {signal handler} named with an \c on<Property>Changed
|
---|
| 231 | syntax. For example, the \l Rectangle element has \l {Item::}{width} and \l {Rectangle::}{color}
|
---|
| 232 | properties. 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
|
---|
| 234 | properties are modified:
|
---|
| 235 |
|
---|
| 236 | \qml
|
---|
| 237 | Rectangle {
|
---|
| 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 |
|
---|
| 245 | Signal handlers are explained further \l {Signal Handlers}{below}.
|
---|
| 246 |
|
---|
| 247 |
|
---|
| 248 | \section2 List properties
|
---|
| 249 |
|
---|
| 250 | List properties look like this:
|
---|
| 251 |
|
---|
| 252 | \code
|
---|
| 253 | Item {
|
---|
| 254 | children: [
|
---|
| 255 | Image {},
|
---|
| 256 | Text {}
|
---|
| 257 | ]
|
---|
| 258 | }
|
---|
| 259 | \endcode
|
---|
| 260 |
|
---|
| 261 | The list is enclosed in square brackets, with a comma separating the
|
---|
| 262 | list elements. In cases where you are only assigning a single item to a
|
---|
| 263 | list, you can omit the square brackets:
|
---|
| 264 |
|
---|
| 265 | \code
|
---|
| 266 | Image {
|
---|
| 267 | children: Rectangle {}
|
---|
| 268 | }
|
---|
| 269 | \endcode
|
---|
| 270 |
|
---|
| 271 | Items in the list can be accessed by index. See the \l{list}{list type} documentation
|
---|
| 272 | for more details about list properties and their available operations.
|
---|
| 273 |
|
---|
| 274 |
|
---|
| 275 | \section2 Default properties
|
---|
| 276 |
|
---|
| 277 | Each object type can specify one of its list or object properties as its default property.
|
---|
| 278 | If a property has been declared as the default property, the property tag can be omitted.
|
---|
| 279 |
|
---|
| 280 | For example this code:
|
---|
| 281 | \code
|
---|
| 282 | State {
|
---|
| 283 | changes: [
|
---|
| 284 | PropertyChanges {},
|
---|
| 285 | PropertyChanges {}
|
---|
| 286 | ]
|
---|
| 287 | }
|
---|
| 288 | \endcode
|
---|
| 289 |
|
---|
| 290 | can be simplified to:
|
---|
| 291 |
|
---|
| 292 | \code
|
---|
| 293 | State {
|
---|
| 294 | PropertyChanges {}
|
---|
| 295 | PropertyChanges {}
|
---|
| 296 | }
|
---|
| 297 | \endcode
|
---|
| 298 |
|
---|
| 299 | because \c changes is the default property of the \c State type.
|
---|
| 300 |
|
---|
| 301 | \section2 Grouped Properties
|
---|
| 302 | \target dot properties
|
---|
| 303 |
|
---|
| 304 | In some cases properties form a logical group and use a 'dot' or grouped notation
|
---|
| 305 | to show this.
|
---|
| 306 |
|
---|
| 307 | Grouped properties can be written like this:
|
---|
| 308 | \qml
|
---|
| 309 | Text {
|
---|
| 310 | font.pixelSize: 12
|
---|
| 311 | font.bold: true
|
---|
| 312 | }
|
---|
| 313 | \endqml
|
---|
| 314 |
|
---|
| 315 | or like this:
|
---|
| 316 | \qml
|
---|
| 317 | Text {
|
---|
| 318 | font { pixelSize: 12; bold: true }
|
---|
| 319 | }
|
---|
| 320 | \endqml
|
---|
| 321 |
|
---|
| 322 | In the element documentation grouped properties are shown using the 'dot' notation.
|
---|
| 323 |
|
---|
| 324 | \section2 Attached Properties
|
---|
| 325 | \target attached-properties
|
---|
| 326 |
|
---|
| 327 | Some objects attach properties to another object. Attached Properties
|
---|
| 328 | are of the form \e {Type.property} where \e Type is the type of the
|
---|
| 329 | element that attaches \e property.
|
---|
| 330 |
|
---|
| 331 | For example, the \l ListView element attaches the \e ListView.isCurrentItem property
|
---|
| 332 | to each delegate it creates:
|
---|
| 333 |
|
---|
| 334 | \code
|
---|
| 335 | Component {
|
---|
| 336 | id: myDelegate
|
---|
| 337 | Text {
|
---|
| 338 | text: "Hello"
|
---|
| 339 | color: ListView.isCurrentItem ? "red" : "blue"
|
---|
| 340 | }
|
---|
| 341 | }
|
---|
| 342 | ListView {
|
---|
| 343 | delegate: myDelegate
|
---|
| 344 | }
|
---|
| 345 | \endcode
|
---|
| 346 |
|
---|
| 347 | Another example of attached properties is the \l Keys element which
|
---|
| 348 | attaches properties for handling key presses to
|
---|
| 349 | any visual Item, for example:
|
---|
| 350 |
|
---|
| 351 | \code
|
---|
| 352 | Item {
|
---|
| 353 | focus: true
|
---|
| 354 | Keys.onSelectPressed: console.log("Selected")
|
---|
| 355 | }
|
---|
| 356 | \endcode
|
---|
| 357 |
|
---|
| 358 | \section1 Signal Handlers
|
---|
| 359 |
|
---|
| 360 | Signal handlers allow JavaScript code to be executed in response to an event. For
|
---|
| 361 | example, the \l MouseArea element has an \l {MouseArea::}{onClicked} handler that can
|
---|
| 362 | be used to respond to a mouse click. Below, we use this handler to print a
|
---|
| 363 | message whenever the mouse is clicked:
|
---|
| 364 |
|
---|
| 365 | \code
|
---|
| 366 | Item {
|
---|
| 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 |
|
---|
| 378 | All signal handlers begin with \e "on".
|
---|
| 379 |
|
---|
| 380 | Some signal handlers include an optional parameter. For example
|
---|
| |
---|