source: trunk/doc/src/declarative/tutorial.qdoc@ 1036

Last change on this file since 1036 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: 9.2 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 qml-tutorial.html
30\title QML Tutorial
31\brief An introduction to the basic concepts and features of QML.
32\nextpage QML Tutorial 1 - Basic Types
33
34This tutorial gives an introduction to QML, the mark up language for Qt Quick. It doesn't cover everything;
35the emphasis is on teaching the key principles, and features are introduced as needed.
36
37Through the different steps of this tutorial we will learn about QML basic types, we will create our own QML component
38with properties and signals, and we will create a simple animation with the help of states and transitions.
39
40Chapter one starts with a minimal "Hello world" program and the following chapters introduce new concepts.
41
42The tutorial's source code is located in the $QTDIR/examples/declarative/tutorials/helloworld directory.
43
44Tutorial chapters:
45
46\list 1
47\o \l {QML Tutorial 1 - Basic Types}{Basic Types}
48\o \l {QML Tutorial 2 - QML Components}{QML Components}
49\o \l {QML Tutorial 3 - States and Transitions}{States and Transitions}
50\endlist
51
52*/
53
54/*!
55\page qml-tutorial1.html
56\title QML Tutorial 1 - Basic Types
57\contentspage QML Tutorial
58\previouspage QML Tutorial
59\nextpage QML Tutorial 2 - QML Component
60
61This first program is a very simple "Hello world" example that introduces some basic QML concepts.
62The picture below is a screenshot of this program.
63
64\image declarative-tutorial1.png
65
66Here is the QML code for the application:
67
68\snippet examples/declarative/tutorials/helloworld/tutorial1.qml 0
69
70\section1 Walkthrough
71
72\section2 Import
73
74First, we need to import the types that we need for this example. Most QML files will import the built-in QML
75types (like \l{Rectangle}, \l{Image}, ...) that come with Qt, using:
76
77\snippet examples/declarative/tutorials/helloworld/tutorial1.qml 3
78
79\section2 Rectangle element
80
81\snippet examples/declarative/tutorials/helloworld/tutorial1.qml 1
82
83We declare a root element of type \l{Rectangle}. It is one of the basic building blocks you can use to create an application in QML.
84We give it an \c{id} to be able to refer to it later. In this case, we call it "page".
85We also set the \c width, \c height and \c color properties.
86The \l{Rectangle} element contains many other properties (such as \c x and \c y), but these are left at their default values.
87
88\section2 Text element
89
90\snippet examples/declarative/tutorials/helloworld/tutorial1.qml 2
91
92We add a \l Text element as a child of the root Rectangle element that displays the text 'Hello world!'.
93
94The \c y property is used to position the text vertically at 30 pixels from the top of its parent.
95
96The \c anchors.horizontalCenter property refers to the horizontal center of an element.
97In this case, we specify that our text element should be horizontally centered in the \e page element (see \l{anchor-layout}{Anchor-based Layout}).
98
99The \c font.pointSize and \c font.bold properties are related to fonts and use the \l{dot properties}{dot notation}.
100
101
102\section2 Viewing the example
103
104To view what you have created, run the \l{QML Viewer} tool (located in the \c bin directory) with your filename as the first argument.
105For example, to run the provided completed Tutorial 1 example from the install location, you would type:
106
107\code
108bin/qmlviewer $QTDIR/examples/declarative/tutorials/helloworld/tutorial1.qml
109\endcode
110*/
111
112/*!
113\page qml-tutorial2.html
114\title QML Tutorial 2 - QML Components
115\contentspage QML Tutorial
116\previouspage QML Tutorial 1 - Basic Types
117\nextpage QML Tutorial 3 - States and Transitions
118
119This chapter adds a color picker to change the color of the text.
120
121\image declarative-tutorial2.png
122
123Our color picker is made of six cells with different colors.
124To avoid writing the same code multiple times for each cell, we create a new \c Cell component.
125A component provides a way of defining a new type that we can re-use in other QML files.
126A QML component is like a black-box and interacts with the outside world through properties, signals and functions and is generally
127defined in its own QML file. (For more details, see \l {Defining New Components}).
128The component's filename must always start with a capital letter.
129
130Here is the QML code for \c Cell.qml:
131
132\snippet examples/declarative/tutorials/helloworld/Cell.qml 0
133
134\section1 Walkthrough
135
136\section2 The Cell Component
137
138\snippet examples/declarative/tutorials/helloworld/Cell.qml 1
139
140The root element of our component is an \l Item with the \c id \e container.
141An \l Item is the most basic visual element in QML and is often used as a container for other elements.
142
143\snippet examples/declarative/tutorials/helloworld/Cell.qml 4
144
145We declare a \c cellColor property. This property is accessible from \e outside our component, this allows us
146to instantiate the cells with different colors.
147This property is just an alias to an existing property - the color of the rectangle that compose the cell (see \l{Adding Properties}).
148
149\snippet examples/declarative/tutorials/helloworld/Cell.qml 5
150
151We want our component to also have a signal that we call \e clicked with a \e cellColor parameter of type \e color.
152We will use this signal to change the color of the text in the main QML file later.
153
154\snippet examples/declarative/tutorials/helloworld/Cell.qml 2
155
156Our cell component is basically a colored rectangle with the \c id \e rectangle.
157
158The \c anchors.fill property is a convenient way to set the size of an element.
159In this case the rectangle will have the same size as its parent (see \l{anchor-layout}{Anchor-based Layout}).
160
161\snippet examples/declarative/tutorials/helloworld/Cell.qml 3
162
163In order to change the color of the text when clicking on a cell, we create a \l MouseArea element with
164the same size as its parent.
165
166A \l MouseArea defines a signal called \e clicked.
167When this signal is triggered we want to emit our own \e clicked signal with the color as parameter.
168
169\section2 The main QML file
170
171In our main QML file, we use our \c Cell component to create the color picker:
172
173\snippet examples/declarative/tutorials/helloworld/tutorial2.qml 0
174
175We create the color picker by putting 6 cells with different colors in a grid.
176
177\snippet examples/declarative/tutorials/helloworld/tutorial2.qml 1
178
179When the \e clicked signal of our cell is triggered, we want to set the color of the text to the \e cellColor passed as a parameter.
180We can react to any signal of our component through a property of the name \e 'onSignalName' (see \l{Signal Handlers}).
181*/
182
183/*!
184\page qml-tutorial3.html
185\title QML Tutorial 3 - States and Transitions
186\contentspage QML Tutorial
187\previouspage QML Tutorial 2 - QML Component
188
189In this chapter, we make this example a little bit more dynamic by introducing states and transitions.
190
191We want our text to move to the bottom of the screen, rotate and become red when clicked.
192
193\image declarative-tutorial3_animation.gif
194
195Here is the QML code:
196
197\snippet examples/declarative/tutorials/helloworld/tutorial3.qml 0
198
199\section1 Walkthrough
200
201\snippet examples/declarative/tutorials/helloworld/tutorial3.qml 2
202
203First, we create a new \e down state for our text element.
204This state will be activated when the \l MouseArea is pressed, and deactivated when it is released.
205
206The \e down state includes a set of property changes from our implicit \e {default state}
207(the items as they were initially defined in the QML).
208Specifically, we set the \c y property of the text to \c 160, the rotation to \c 180 and the \c color to red.
209
210\snippet examples/declarative/tutorials/helloworld/tutorial3.qml 3
211
212Because we don't want the text to appear at the bottom instantly but rather move smoothly,
213we add a transition between our two states.
214
215\c from and \c to define the states between which the transition will run.
216In this case, we want a transition from the default state to our \e down state.
217
218Because we want the same transition to be run in reverse when changing back from the \e down state to the default state,
219we set \c reversible to \c true.
220This is equivalent to writing the two transitions separately.
221
222The \l ParallelAnimation element makes sure that the two types of animations (number and color) start at the same time.
223We could also run them one after the other by using \l SequentialAnimation instead.
224
225For more details on states and transitions, see \l {QML States} and the \l{declarative/animation/states}{states and transitions example}.
226*/
Note: See TracBrowser for help on using the repository browser.