source: trunk/doc/src/declarative/qml-intro.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: 26.4 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
30/*!
31\page qml-intro.html
32\title Intro to Qt Quick
33
34\section1 Overview
35
36QML is a high level, scripted language. Its commands, more correctly \e elements,
37leverage the power and efficiency of the Qt libraries to make easy to use
38commands that perform intuitive functions. Draw a rectangle, display an image at
39a position and so on. Behind these elements are complex C++ libraries that
40efficiently perform the action. As with any graphical application, always
41consider that this ability to easily build graphically rich applications means
42that some care may be needed to prevent performance problems.
43
44The language also allows more flexibility of these commands by using
45Javascript rather than C++ to add new layers of logic to your application.
46Javascript is easier to learn than C++ and can be embedded into the QML
47files or imported from a separate file.
48
49\bold{In QML the types of various 'objects' are referred to as \l {QML
50Elements}{elements}}.
51
52An element usually has various \e properties that help define the element. For
53example, if we created an element called Circle then the radius of the circle
54would be a property.
55
56
57\section1 A First Look
58
59The basic syntax of an \l{QML Elements}{element} is
60
61\qml
62SomeElement {
63 id: myObject
64 ... some other things here ...
65}
66\endqml
67
68Here we are defining a new object. We specify its 'type' first as SomeElement.
69Then within matching braces { ... } we specify the various parts of our
70element.
71
72The \c id is a unique identifier for the element, it must start with a lower
73case letter and only contain letters, numbers and underscores. It is this
74particular object's name. If this SomeElement \l {QML Elements}{element} was
75a Rectangle instead and it was one of many then the \e optional unique id
76would allow us to manipulate each element individually.
77
78Each visual element is ultimately based on, or inherits from, an element
79called \l Item. \l Item has certain properties and actions that may be
80useful. The properties have default values so you need only specify the
81ones you will need.
82
83Take a simple element such as a \l Rectangle. It has an \c id, we will call
84it \e myRectangle, it has a \c width and a \c height. Imagine that we
85want a rectangle that is 500 pixels by 400 pixels in the x and y directions
86(horizontal by vertical).
87
88We can implement this \l Rectangle with these properties this way
89
90\snippet doc/src/snippets/declarative/qml-intro/rectangle.qml document
91
92This is a valid QML script. To run it, copy it and save it to a file, say
93myexample.qml, and on the command line run the following command:
94
95\code
96qmlviewer myexample.qml
97\endcode
98
99On Mac OS X, open the "QMLViewer" application instead and open the
100\c myexample.qml file, or run it from the command line:
101
102\code
103QMLViewer.app/Contents/MacOS/QMLViewer myexample.qml
104\endcode
105
106It will create a very boring rectangle in its own window.
107
108
109\section1 Hello World!
110
111We can now add some color and text to make a Hello World QML program.
112
113\l Rectangle has the property \l{Rectangle::color}{color} to produce a
114background color.
115
116Text is handled by a different element called \l Text. We need to create a
117\l Text object inside the \l Rectangle and set its \l{Text::}{text}
118property to "Hello World!". So to set the text to "Hello world" and the
119background colour to light gray,
120
121\snippet doc/src/snippets/declarative/qml-intro/hello-world1.qml document
122
123
124\section1 Hello World Again
125
126From now on we will not always show the import statement for Qt but it
127should still be there when you create your QML scripts.
128
129To make our Hello World example a little nicer set the position of the text
130to be at pixel position x = 100, y = 100 within the displayed window. This
131position belongs to the \l Text element so we set the position inside its
132definition. Note that we separate different QML statements on the same line
133with a semi-colon, or we could have simply put each statement on a new line
134
135\snippet doc/src/snippets/declarative/qml-intro/hello-world2.qml updated text
136
137Not only did we reposition the text, but the text was altered by adding
138HTML tags to change the font size. The text color was also changed from the
139default black to dark green by using a standard string for the color's SVG
140name.
141
142We could also have used a hexadecimal string for the RGB (red-green-blue, as
143#rrggbb) values of the color similar to the method used in HTML. For
144example, mostly blue with a green tint,
145
146\snippet doc/src/snippets/declarative/qml-intro/hello-world3.qml updated text
147
148All of these changes occurred within the \l Text object which is the scope
149of these property changes.
150
151Other objects may use the information but it belongs to the element where
152the property has been defined.
153
154
155\section1 Images
156
157To add an image to our little application we use the \l Image element. An
158\l Image uses a path to an image file, and has properties to control
159the aspect ratio, the image size, to tile the area amongst others. The
160source of the image, the path to the file, is a URL. Therefore the file can
161be local: \e {mydir/myimage1.png}. Or it can be remote:
162\e {"http://www.example.com/images/myimage1.png"}.
163
164\snippet doc/src/snippets/declarative/qml-intro/hello-world4.qml added an image
165
166This displays the image, as we would expect, at the top left of the window.
167The position of the default x = 0, y = 0 coordinate. The example here uses
168a PNG file, but it could have been one of various supported formats,
169including JPG and GIF.
170
171Let us reposition the image and enlarge it. Place it at the same 'x' offset
172as the "Hello world again" text, but put it another 50 pixels below the
173text, also make it 150 by 150 pixels in size,
174
175\snippet doc/src/snippets/declarative/qml-intro/hello-world5.qml positioning the image
176
177Adding the Hello World example, with the text and the image example we can
178write a simple piece of QML that starts to look a bit better.
179
180\snippet doc/src/snippets/declarative/qml-intro/hello-world5.qml document
181
182The result is still quite simple
183
184\image qml-intro-helloa.png
185
186
187\section1 Anchors: Aligning Elements
188
189Using absolute positioning, such as saying x = 100 and y = 150, works well
190until the user or developer stretches or increases the size of the window.
191Then the positions need to be recalculated. What would be nice would be a
192relative means of positioning of objects in a window or rectangle. For
193example, we want to place an image at the bottom of a rectangle, we would
194like to specify the image's location as the 'bottom of the window', not a
195specific coordinate. We can do this with the anchors property, which
196objects inherit from Item.
197
198The anchors property is really a property group. It is a collection of
199related properties. It has properties within it which can be used by means
200of the dot notation.
201
202The dot notation uses object \c{id}s and property names to use a particular
203object or property. Say I have a rectangle r1, which contains a rectangle
204r2, which contains an Item item1, which has an 'x' property I want to
205change. I just use the dot notation to identify it: r1.r2.item1.x
206
207If we want to position an image at the bottom of the rectangle it is
208inside. I have to specify that the bottom of the image is also at the
209bottom of the rectangle
210
211\snippet doc/src/snippets/declarative/qml-intro/anchors1.qml document
212
213This places the logo at the bottom left of the window.
214
215\image qml-intro-anchors1.png "A simple anchor"
216
217We would like it centered and not touching the bottom of the window, for
218aesthetic reasons. For the centering we use the horizontalCenter property,
219and to prevent the touching of the image to the bottom of the rectangle,
220the bottomMargin property is used. So the new actions for the script are
221
222 \list
223 \o set the bottom of the image (anchors.bottom) to be the bottom of the window
224 \o move the image to be in the horizontal center of the window
225 \o set a margin of 10 pixels so that the image does not touch the bottom window border
226 \endlist
227
228Encoded into QML the script becomes
229
230\snippet doc/src/snippets/declarative/qml-intro/anchors2.qml document
231
232Run this and resize the window. You will see that now the position of the
233image adjusts during the resize.
234
235\image qml-intro-anchors2.png "Image Centered at the Bottom"
236
237You can also add another object say a block of descriptive text and place
238it above or below the image or to the side. This code places some text just
239above the image
240
241\snippet doc/src/snippets/declarative/qml-intro/anchors3.qml adding some text
242
243\image qml-intro-anchors3.png
244
245\note \e anchors is a property group, to be used within the object. When
246referencing these properties from another object we use the property
247directly, instead of saying:
248
249\qml
250myRectangle.anchors.top // Wrong
251\endqml
252
253we use
254
255\qml
256myRectangle.top // Correct
257\endqml
258
259
260\section1 Transformations
261
262We can transform a graphical object to get additional effects. Rotate a
263piece of text by 180 degrees to display upside-down text. Rotate an image
264by 90 degrees to lay it on its side. These transformations require
265additional information.
266
267For rotation, the additional information includes: the origin relative to
268the object being rotated, the axis of rotation, and the angle in degrees to
269rotate the image through in a clockwise direction. The axis does not have
270to be the z-axis, the line between your eyes and the image, it could be
271along the vertical y-axis or the horizontal x-axis. We have three
272dimensions to play with. For simplicity in this example we will rotate
273about the z-axis by 90 degrees in a negative direction, anti-clockwise.
274
275Rotation of text was also suggested. It could also be useful to scale the
276text. We can do both. The \l {Item::transform}{transform} property is a
277\e list of \l Transform elements, so using the list syntax
278
279\qml
280myList: [ listElement1, listElement2, ... } ]
281\endqml
282
283we can produce a list of transformations.
284
285The text will be rotated by 45 degrees anti-clockwise and scaled
286vertically by a factor of 1.5 and by 1.2 horizontally.
287
288Using the example above as the basis for this we have,
289
290\snippet doc/src/snippets/declarative/qml-intro/transformations1.qml document
291
292The code block in \c image1 starting with \c transform specifies that the
293\l {Item::transform}{transform} property will be a Rotation through -90
294degrees, which is anti-clockwise, about the z-axis running through the
295center of the image at (75,75), since the image is 150 x 150 pixels.
296
297The other transformation available is \l Translate. This produces a change
298in position of the item.
299
300\note In a list of transformations the order of the transformations is
301important. In the above example try swapping around the Scale transform with
302the Rotation transform, remember to remove or add the comma. The results are
303acceptable for our little test but not the same.
304
305
306\section1 Animations
307
308Animation in QML is done by animating properties of objects. Properties
309that are numbers, colors, Rectangles, points and directions. In QML these
310are \l {QML Basic Types} named as real, int, color, rect, point, size, and
311vector3d. There are a number of different ways to do animation. Here we
312will look at a few of them.
313
314\section2 Number Animation
315
316Previously we have used a rotation transformation to change the orientation
317of an image. We could easily animate this rotation so that instead of a
318straight rotation counter-clockwise of 90 degrees we could rotate the image
319through a full 360 degrees in an animation. The axis of rotation wont
320change, the position of the center of the image will not change, only the
321angle will change. Therefore, a NumberAnimation of a rotation's angle should
322be enough for the task. If we wish for a simple rotation about the center
323of the image then we can use the \c rotation property that is inherited
324from \l Item. The rotation property is a real number that specifies the
325angle in a clockwise direction for the rotation of the object. Here is the
326code for our animated rotating image.
327
328\snippet doc/src/snippets/declarative/qml-intro/number-animation1.qml document
329
330The \c {transformOrigin: Item.Center} is redundant since this is the default
331axis of rotation anyway. But if you change \c Center to \c BottomRight you
332will see an interesting variation.
333
334Also if instead the \l Rotation transformation had been used then we would have
335more control over the various parameters. We could vary the axis, to be not
336just a different offset from the z-axis but along the y-axis, x-axis or
337combination. For example, if the task had been to animate the rotation
338about the y-axis passing through the center of the image then the following
339code would do it.
340
341\snippet doc/src/snippets/declarative/qml-intro/number-animation2.qml document
342
343Here there is a rectangle 600 by 400 pixels. Placed within that rectangle
344is an image 100 by 100 pixels. It is rotated about the center of the image
345about the y-axis so that it looks as if it is rotating about an invisible
346vertical string holding it up. The time it takes to complete the rotation is 3
347seconds (3,000 milliseconds). The NumberAnimation is applied to the angle
348taking it from 0 (no change) to 360 degrees, back where it started.
349Strictly speaking it isn't necessary to go from 0 to 360 since the same
350location is duplicated, but it makes it easier to read in this example and
351it has no visible effect on the animation. The number of loops that the
352animation will execute is set to \c {Animation.Infinite} which means that the
353animation is in an endless loop.
354
355To see an interesting variation. Change the axis to \c {axis { x:1; y:1; z:1
356}}. This is a line coming from the center of the image downwards to the
357right and out of the screen. Although the change is simple the rotation
358seems complex.
359
360\section2 Sequential Animation
361
362For a more complex animation we will need two images. The first image will
363be placed at the center of a window (Rectangle) and the second image will
364be at the upper left of the window. The animation will move the second
365image from the top left of the window to the bottom right. In doing so we
366will be animating the position and the size of the image.
367
368First create two images
369
370\snippet doc/src/snippets/declarative/qml-intro/sequential-animation1.qml document
371
372We will add to 'image1' a SequentialAnimation from x = 20 to the target of
373x = 450. The 'from' values will be used because we will be repeating the
374animation, so the object needs to know where the original position is, both
375x and y. The SequentialAnimation of x will set it to repeat by indicating
376that the number of animation loops is infinite, meaning that the 'loop'
377counter will be set to a value Animation.Infinite that indicates an endless
378cycle. Also there will be a NumberAnimation to vary the numeric property
379between the x values and over a given duration. After the NumberAnimation
380there will be a PauseAnimation that will pause the animation for 500
381milliseconds (half a second) simply for the visual effect.
382
383\snippet doc/src/snippets/declarative/qml-intro/sequential-animation2.qml adding a sequential animation
384
385A similar block of code is written for the animation of the 'y' value of
386the position.
387
388We will also animate the scale of the object, so as it goes from top left
389to bottom right of the window it will become smaller until about midway,
390and then become larger. To complete the animation we will set the 'z'
391values of the images. 'z' is the stacking order, the z-axis effectively
392points out from the screen to your eyes with the default value of 'z' being
3930. So if we set the Rectangle to have z with value zero, just to be sure,
394and image1 to 1 and image2 to 2 then image2 will be in the foreground and
395image1 in the background. When image1 passes image2 it will pass behind it.
396The completed code looks like
397
398\snippet doc/src/snippets/declarative/qml-intro/sequential-animation3.qml document
399
400The \c {easing.type} has many options, expressed as a string. It specifies the
401kind of equation that describes the acceleration of the property value, not
402necessarily position, over time.
403
404For example, \e InOutQuad means that at the start and the end of the animation the
405'velocity' is low but the acceleration or deceleration is high. Much like a car
406accelerating from stop, and decelerating to stop at the end of a journey,
407with the maximum speed being in the middle. Examine the \l {PropertyAnimation::easing.type}
408{easing} documentation and the various graphs that show the effect. The horizontal
409axis, 'progress', can be thought of as time. The vertical axis is the value
410of the particular property.
411
412In discussing animation we need to describe three objects: State, MouseArea
413and Signals. Although independent of the animation elements, animation
414delivers some of the best examples that illustrate these new elements.
415
416
417
418\section2 Animation Summary
419
420\table
421 \header
422 \o Name
423 \o Description
424 \row
425 \o PropertyAnimation
426 \o a property value on a target object is varied to a specified value over a given time.
427
428 \row
429 \o NumberAnimation
430 \o animate a numeric property from one value to another over a given time.
431
432 \row
433 \o PauseAnimation
434 \o results in the task waiting for the specified duration, in milliseconds.
435
436 \row
437 \o SequentialAnimation
438 \o allows us to list in order the animation events we want to occur, first A then B then C and so on.
439
440 \row
441 \o ParallelAnimation
442 \o enables us to run different animations at the same time instead of sequentially.
443
444\endtable
445
446
447\section1 Using States
448
449A state is a defined set of values in the configuration of an object and
450often depends on the previous state. For example, a glass could be in a
451state we call 'HalfFull' if it is being filled with a liquid and has
452reached half of its total capacity. We could also have a state called
453HalfEmpty which is the state that occurs when the amount of liquid drops to
454half of the glass's capacity. Both states represent the same amount of
455liquid, but we consider them different. Likewise, states in a program
456represent not just values but may include how the current values were
457reached.
458
459When a state changes a \e transition occurs. This is an opportunity to make
460changes or take actions that depend on the movement to the new state. For
461example, if we had a scene in the country where the state variable has two
462states "daylight" and "night". Then when the state changes to "night" at
463this transition the sky would be made dark, stars would be shown, the
464countryside would be darkened. And when the state changes to "daylight" the
465opposite changes would be made: the sky is now blue, the scenery is green,
466there is a sun in the sky.
467
468Here is a simple QML program that shows the change of state in the above
469example. We have two rectangles, the top one is the 'sky' and the bottom
470one is the 'ground'. We will animate the change from daylight to night.
471There will be two states, but we only need to define one since 'daylight'
472will be the default state. We will just go to 'night' by clicking and
473holding the left mouse button down, releasing the mouse button will reverse
474the process
475
476\snippet doc/src/snippets/declarative/qml-intro/states1.qml document
477
478Several new things appear in this sample. Firstly, we use a \l MouseArea
479element to detect mouse clicks in the \e mainRectangle. Secondly, we use
480the list notation [ thing1 , thing2, ... ] to build a list of states and a
481list of transitions.
482
483\l MouseArea defines a region that will respond to mouse clicks. In this case
484we are only concerned with when the mouse is pressed or not pressed, not
485the particular button or other details. The area of the MouseArea is the
486entire main window, mainRectangle, so that clicking anywhere in this region
487will start the animation. Since we are using the 'pressed' mouse state,
488then the animation will move from 'daylight' to 'night' only while the mouse
489button remains pressed.
490
491When the button is released the 'daylight' state is entered and the
492transition from 'night' to 'daylight' is triggered causing the animation to
493run. The transition specifies the duration in milliseconds of the
494ColorAnimation, while the state specifies the color of the new state.
495
496The PropertyChanges command is the way that we nominate which properties
497will change in a change of state, and what new value the property will
498take. Since, for example, we want the 'sky' region to turn to dark blue and
499the 'ground' region to turn to black for the 'night' state, then the
500rectangles for those regions are the 'target' and the property in the target
501is 'color'.
502
503
504\section1 Signals
505
506Signals are simply events that can be hooked up to actions we want performed.
507In QML they are usually preceded by the word 'on', for example in the animation
508using a MouseArea the signal was \l {MouseArea::onPressed}{onPressed}. If
509you look at the C++ documentation you will see a lot of talk about
510\l {Signals & Slots}{Signals and Slots}. Signals are connected to Slots. The
511signal represents an event and the Slot is the function that does something
512based on that event. You can also have Signals connected to other Signals, so
513that one Signal (event) triggers another Signal (event), and so forth. It is
514nice to know this is what happens beneath the QML layer but not essential for
515using QML.
516
517Most elements do not have Signals associated with them. However, a few like
518the \l Audio element have many signals. Some of the \l Audio signals are
519used to represent events such as when the audio is stopped, play is pressed,
520paused, and reaching the end of the media. They allow the developer to connect,
521 for example, the press of a user interface button (perhaps a MouseArea) to
522 some QML that will handle this event.
523
524
525\section1 Analyzing An Example: Dial Control
526
527In the Qt \e {examples/declarative/ui-components} folder you will find a folder
528\e {dialcontrol} which contains the \e dialcontrol example.
529
530\image qml-dial.png "QML Dial example with Slider"
531
532In essence this small application has a sliding bar that you can slide using
533a mouse, and a graphical dial that responds to the position of the slider.
534
535The code for the example is in two parts: Dial.qml and dialcontrol.qml.
536
537\e {Dial.qml} can be found in the \e content sub-directory. It defines a \c Dial
538component similar to an odometer. Eventually, the example will hook up a slider
539component so that moving the slider will change the position of a needle on the
540dial.
541
542The code for the \c Dial, identified by the name of the file, contains four images
543in overlapping order: the background (numbers and divisions), the shadow of the
544needle, the needle itself, and finally the 'glass' overlay (containing
545transparent layers).
546
547The \c needle_shadow.png image has a \l Rotation assigned to the \e transform
548attribute of the \l Image. The rotation is set to match the angle of the needle
549image angle value \e {needleRotation.angle}. Both the needle and the
550needle_shadow have the same default \e x and \e y values but the rotation origin
551for the needle is slightly different so that a shadow will be evident as the
552needle moves.
553
554\snippet examples/declarative/ui-components/dialcontrol/content/Dial.qml needle_shadow
555
556And the needle
557
558\snippet examples/declarative/ui-components/dialcontrol/content/Dial.qml needle
559
560The final image is the overlay which simply has a position defined.
561
562\snippet examples/declarative/ui-components/dialcontrol/content/Dial.qml overlay
563
564\e {dialcontrol.qml} in the \e {examples/declarative/ui-components/dialcontrol} directory is the
565main file of the example. It defines the visual environment that the Dial
566will fit into. Because the \e Dial component and the images live in the \e
567content sub-directory we will have to import this into \e dialcontrol.qml. So the
568start of the file looks like
569
570\snippet examples/declarative/ui-components/dialcontrol/dialcontrol.qml imports
571
572The visual space is bound by a 300 by 300 pixel \l Rectangle which is given
573a gray color. Inside this rectangle is our component \e Dial and a \l Rectangle.
574Inside the rectangle called 'container' is another rectangle with the
575interesting name 'slider'.
576
577\snippet examples/declarative/ui-components/dialcontrol/dialcontrol.qml 0
578
579The Dial component, named 'dial, is \e anchored to the center of the main
580rectangle. The \c value attribute of 'dial' is set to a value based on the
581'slider' horizontal position and the 'container' width. So changes to the
582'slider' position will change the Dial \c value which is used in Dial to compute
583the rotation of the needle image. Notice this piece of code in Dial where
584the change in \c value modifies the position of the needle.
585
586\snippet examples/declarative/ui-components/dialcontrol/content/Dial.qml needle angle
587
588This is part of the \c needleRotation that rotates the needle and causes the
589rotation of its shadow. \l SpringAnimation is an element that modifies the value
590of that rotation \e angle and mimics the oscillatory behavior of a spring,
591with the appropriate \e spring constant to control the acceleration and the \e
592damping to control how quickly the effect dies away.
593
594The 'container' is light gray with a color gradient defined using
595\l GradientStop. The gradient is applied vertically. If you need a horizontal
596gradient then you could apply the vertical gradient and then rotate the item
597by 90 degrees.
598
599The 'slider' is dark gray and also has a vertical color gradient. The most
600important thing about the 'slider' is that it has a MouseArea defined, which
601specifies a \c {drag.target} on itself along the X-axis. With minimum
602and maximum values on the X-axis defined. So we can click on the 'slider' and
603drag it left and right within the confines of the 'container'. The motion of
604the 'slider' will then change the \c value attribute in \e Dial as discussed
605already.
606
607Also notice the use of a \c radius value for a rectangle. This produces rounded
608corners. That is how the 'container' and 'slider' are displayed with a
609pleasant rounded look.
610
611
612
613*/
614
615
616
Note: See TracBrowser for help on using the repository browser.