755e537e434422784ad723f0dfe02e9a97240d7a
[profile/ivi/qtdeclarative.git] / doc / src / snippets / qtquick1 / properties.qml
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the documentation of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:BSD$
10 ** You may use this file under the terms of the BSD license as follows:
11 **
12 ** "Redistribution and use in source and binary forms, with or without
13 ** modification, are permitted provided that the following conditions are
14 ** met:
15 **   * Redistributions of source code must retain the above copyright
16 **     notice, this list of conditions and the following disclaimer.
17 **   * Redistributions in binary form must reproduce the above copyright
18 **     notice, this list of conditions and the following disclaimer in
19 **     the documentation and/or other materials provided with the
20 **     distribution.
21 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22 **     the names of its contributors may be used to endorse or promote
23 **     products derived from this software without specific prior written
24 **     permission.
25 **
26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 //! [document]
42 import QtQuick 1.0
43
44 //! [parent begin]
45 Rectangle {
46 //! [parent begin]
47
48     //! [inherited properties]
49     width: 320; height: 240
50     color: "lightblue"
51     focus: true
52     //! [inherited properties]
53
54     //! [custom properties]
55     property int counter
56     property real area: 100.45
57     //! [custom properties]
58
59     //! [property types]
60     property int number
61     property real volume: 100.45
62     property date today: "2011-01-01"
63     property color background: "yellow"
64     //! [property types]
65
66
67 //! [grouped properties]
68 Text {
69     //dot notation
70     font.pixelSize: 12
71     font.bold: true
72 }
73
74 Text {
75     //group notation
76     font {pixelSize: 12; bold: true}
77 }
78 //! [grouped properties]
79
80
81 //! [property binding]
82 Rectangle {
83     width: parent.width
84 }
85 //! [property binding]
86
87 //! [property assignment]
88 Rectangle {
89     Component.onCompleted: {
90         width = 150
91     }
92 }
93 //! [property assignment]
94
95 Rectangle {
96     //placeholder slider
97     id: slider
98     property real value
99 }
100 Rectangle {
101     //placeholder system
102     id: system
103     property real brightness
104 }
105 //! [binding element]
106 Binding {
107     target: system
108     property: "brightness"
109     value: slider.value
110 }
111 //! [binding element]
112
113 Rectangle {
114     //placeholder warning
115     id: warning
116     color: "red"
117 }
118 //! [PropertyChanges element]
119 Rectangle {
120     id: rectangle
121
122     states: State {
123         name: "WARNING"
124         PropertyChanges {
125             target: rectangle
126             color: warning.color
127         }
128     }
129 }
130 //! [PropertyChanges element]
131
132 //! [list property]
133 Item {
134     id: multistate
135     states: [
136         State {name: "FETCH"},
137         State {name: "DECODE"},
138         State {name: "EXECUTE"}
139     ]
140 }
141 //! [list property]
142 //! [single property]
143 Item {
144     id: monostate
145     states: State {name: "RUNNING"}
146 }
147 //! [single property]
148
149 Item {
150     id: printstate
151 //! [print list property]
152     Component.onCompleted: console.log (multistate.states[0].name)
153 //! [print list property]
154 }
155
156 //! [JavaScript sample]
157 function calculateArea(width, height) {
158     return (width * height) * 0.5
159 }
160
161 Rectangle {
162     width: 150; height: 75
163     property real area: calculateArea(width, height)
164     property real parentArea: calculateArea(parent.width,parent.height)
165     color: { if (area > parentArea) "blue"; else "red" }
166 }
167 //! [JavaScript sample]
168
169 //! [id property]
170 Rectangle {
171     id: container
172     width: 100; height: 100
173     Rectangle {
174         width: parent.width; height: parent.height
175     }
176 }
177 Rectangle {
178     width: container.width; height: container.height
179 }
180 //! [id property]
181
182 //! [default property]
183 Item {
184     Text {}
185     Rectangle {}
186     Timer {}
187 }
188
189 Item {
190     //without default property
191     children: [
192         Text {},
193         Rectangle {}
194     ]
195     resources: [
196         Timer {}
197     ]
198 }
199 //! [default property]
200
201 //! [state default]
202 State {
203     changes: [
204         PropertyChanges {},
205         PropertyChanges {}
206     ]
207 }
208
209 State {
210     PropertyChanges {}
211     PropertyChanges {}
212 }
213 //! [state default]
214
215 //! [object binding]
216 Rectangle {
217
218     id: parentrectangle
219     gradient:
220         Gradient { //not a child of parentrectangle
221
222             //generates a TypeError
223             //Component.onCompleted: console.log(parent.width)
224         }
225
226     //child of parentrectangle
227     Rectangle {property string name: "childrectangle"}
228
229     //prints "childrectangle"
230     Component.onCompleted: console.log(children[0].name)
231 }
232 //! [object binding]
233
234 //! [list attached property]
235 Component {
236     id: listdelegate
237     Text {
238         text: "Hello"
239         color: ListView.isCurrentItem ? "red" : "blue"
240     }
241 }
242 ListView {
243     delegate: listdelegate
244 }
245 //! [list attached property]
246
247 //! [attached signal handler]
248 Item {
249     Keys.onPressed: console.log("Key Press Detected")
250     Component.onCompleted: console.log("Completed initialization")
251 }
252 //! [attached signal handler]
253
254 //! [alias usage]
255 Button {
256     id: textbutton
257     buttonLabel: "Click Me!"
258 }
259 //! [alias usage]
260
261 //! [image alias]
262 Button {
263     id: imagebutton
264     buttonImage.source: "http://qt.nokia.com/logo.png"
265     buttonLabel: buttonImage.source
266 }
267 //! [image alias]
268
269 Item {
270 id: widget
271
272 //! [alias complete]
273 property alias widgetLabel: label
274
275 //will generate an error
276 //widgetLabel.text: "Initial text"
277
278 //will generate an error
279 //property alias widgetLabelText: widgetLabel.text
280
281 Component.onCompleted: widgetLabel.text = "Alias completed Initialization"
282 //! [alias complete]
283
284     Text {id: label}
285 }
286
287 //![alias overwrite]
288 Rectangle {
289     id: coloredrectangle
290     property alias color: bluerectangle.color
291     color: "red"
292
293     Rectangle {
294         id: bluerectangle
295         color: "#1234ff"
296     }
297
298     Component.onCompleted: {
299         console.log (coloredrectangle.color)    //prints "#1234ff"
300         setInternalColor()
301         console.log (coloredrectangle.color)    //prints "#111111"
302         coloredrectangle.color = "#884646"
303         console.log (coloredrectangle.color)    //prints #884646
304     }
305
306     //internal function that has access to internal properties
307     function setInternalColor() {
308         color = "#111111"
309     }
310 }
311 //![alias overwrite]
312 //! [parent end]
313 }
314 //! [parent end]
315 //! [document]