source: trunk/doc/src/examples/qml-extending.qdoc@ 846

Last change on this file since 846 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.9 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\example declarative/cppextensions/referenceexamples/adding
30\title Extending QML - Adding Types Example
31
32The Adding Types Example shows how to add a new element type, \c Person, to QML.
33The \c Person type can be used from QML like this:
34
35\snippet examples/declarative/cppextensions/referenceexamples/adding/example.qml 0
36
37\section1 Declare the Person class
38
39All QML elements map to C++ types. Here we declare a basic C++ Person class
40with the two properties we want accessible on the QML type - name and shoeSize.
41Although in this example we use the same name for the C++ class as the QML
42element, the C++ class can be named differently, or appear in a namespace.
43
44\snippet examples/declarative/cppextensions/referenceexamples/adding/person.h 0
45
46\section1 Define the Person class
47
48\snippet examples/declarative/cppextensions/referenceexamples/adding/person.cpp 0
49
50The Person class implementation is quite basic. The property accessors simply
51return members of the object instance.
52
53The \c main.cpp file also calls the \c qmlRegisterType() function to
54register the \c Person type with QML as a type in the People library version 1.0,
55and defines the mapping between the C++ and QML class names.
56
57\section1 Running the example
58
59The main.cpp file in the example includes a simple shell application that
60loads and runs the QML snippet shown at the beginning of this page.
61*/
62
63/*!
64\example declarative/cppextensions/referenceexamples/properties
65\title Extending QML - Object and List Property Types Example
66
67This example builds on:
68\list
69\o \l {Extending QML - Adding Types Example}
70\endlist
71
72The Object and List Property Types example shows how to add object and list
73properties in QML. This example adds a BirthdayParty element that specifies
74a birthday party, consisting of a celebrant and a list of guests. People are
75specified using the People QML type built in the previous example.
76
77\snippet examples/declarative/cppextensions/referenceexamples/properties/example.qml 0
78
79\section1 Declare the BirthdayParty
80
81The BirthdayParty class is declared like this:
82
83\snippet examples/declarative/cppextensions/referenceexamples/properties/birthdayparty.h 0
84\snippet examples/declarative/cppextensions/referenceexamples/properties/birthdayparty.h 1
85\snippet examples/declarative/cppextensions/referenceexamples/properties/birthdayparty.h 2
86\snippet examples/declarative/cppextensions/referenceexamples/properties/birthdayparty.h 3
87
88The class contains a member to store the celebrant object, and also a
89QList<Person *> member.
90
91In QML, the type of a list properties - and the guests property is a list of
92people - are all of type QDeclarativeListProperty<T>. QDeclarativeListProperty is simple value
93type that contains a set of function pointers. QML calls these function
94pointers whenever it needs to read from, write to or otherwise interact with
95the list. In addition to concrete lists like the people list used in this
96example, the use of QDeclarativeListProperty allows for "virtual lists" and other advanced
97scenarios.
98
99\section2 Define the BirthdayParty
100
101The implementation of BirthdayParty property accessors is straight forward.
102
103\snippet examples/declarative/cppextensions/referenceexamples/properties/birthdayparty.cpp 0
104
105\section1 Running the example
106
107The main.cpp file in the example includes a simple shell application that
108loads and runs the QML snippet shown at the beginning of this page.
109*/
110
111/*!
112\example declarative/cppextensions/referenceexamples/coercion
113\title Extending QML - Inheritance and Coercion Example
114
115This example builds on:
116\list
117\o \l {Extending QML - Object and List Property Types Example}
118\o \l {Extending QML - Adding Types Example}
119\endlist
120
121The Inheritance and Coercion Example shows how to use base classes to assign
122elements of more than one type to a property. It specializes the Person element
123developed in the previous examples into two elements - a \c Boy and a \c Girl.
124
125\snippet examples/declarative/cppextensions/referenceexamples/coercion/example.qml 0
126
127\section1 Declare Boy and Girl
128
129\snippet examples/declarative/cppextensions/referenceexamples/coercion/person.h 0
130
131The Person class remains unaltered in this example and the Boy and Girl C++
132classes are trivial extensions of it. As an example, the inheritance used here
133is a little contrived, but in real applications it is likely that the two
134extensions would add additional properties or modify the Person classes
135behavior.
136
137\section2 Define People as a base class
138
139The implementation of the People class itself has not changed since the the
140previous example. However, as we have repurposed the People class as a common
141base for Boy and Girl, we want to prevent it from being instantiated from QML
142directly - an explicit Boy or Girl should be instantiated instead.
143
144\snippet examples/declarative/cppextensions/referenceexamples/coercion/main.cpp 0
145
146While we want to disallow instantiating Person from within QML, it still needs
147to be registered with the QML engine, so that it can be used as a property type
148and other types can be coerced to it.
149
150\section2 Define Boy and Girl
151
152The implementation of Boy and Girl are trivial.
153
154\snippet examples/declarative/cppextensions/referenceexamples/coercion/person.cpp 1
155
156All that is necessary is to implement the constructor, and to register the types
157and their QML name with the QML engine.
158
159\section1 Running the example
160
161The BirthdayParty element has not changed since the previous example. The
162celebrant and guests property still use the People type.
163
164\snippet examples/declarative/cppextensions/referenceexamples/coercion/birthdayparty.h 0
165
166However, as all three types, Person, Boy and Girl, have been registered with the
167QML system, on assignment QML automatically (and type-safely) converts the Boy
168and Girl objects into a Person.
169
170The main.cpp file in the example includes a simple shell application that
171loads and runs the QML snippet shown at the beginning of this page.
172*/
173
174/*!
175\example declarative/cppextensions/referenceexamples/default
176\title Extending QML - Default Property Example
177
178This example builds on:
179\list
180\o \l {Extending QML - Inheritance and Coercion Example}
181\o \l {Extending QML - Object and List Property Types Example}
182\o \l {Extending QML - Adding Types Example}
183\endlist
184
185The Default Property Example is a minor modification of the
186\l {Extending QML - Inheritance and Coercion Example} that simplifies the
187specification of a BirthdayParty through the use of a default property.
188
189\snippet examples/declarative/cppextensions/referenceexamples/default/example.qml 0
190
191\section1 Declaring the BirthdayParty class
192
193The only difference between this example and the last, is the addition of the
194\c DefaultProperty class info annotation.
195
196\snippet examples/declarative/cppextensions/referenceexamples/default/birthdayparty.h 0
197
198The default property specifies the property to assign to whenever an explicit
199property is not specified, in the case of the BirthdayParty element the guest
200property. It is purely a syntactic simplification, the behavior is identical
201to specifying the property by name, but it can add a more natural feel in many
202situations. The default property must be either an object or list property.
203
204\section1 Running the example
205
206The main.cpp file in the example includes a simple shell application that
207loads and runs the QML snippet shown at the beginning of this page.
208*/
209
210/*!
211\example declarative/cppextensions/referenceexamples/grouped
212\title Extending QML - Grouped Properties Example
213
214This example builds on:
215\list
216\o \l {Extending QML - Default Property Example}
217\o \l {Extending QML - Inheritance and Coercion Example}
218\o \l {Extending QML - Object and List Property Types Example}
219\o \l {Extending QML - Adding Types Example}
220\endlist
221
222*/
223
224/*!
225\example declarative/cppextensions/referenceexamples/attached
226\title Extending QML - Attached Properties Example
227
228This example builds on:
229\list
230\o \l {Extending QML - Grouped Properties Example}
231\o \l {Extending QML - Default Property Example}
232\o \l {Extending QML - Inheritance and Coercion Example}
233\o \l {Extending QML - Object and List Property Types Example}
234\o \l {Extending QML - Adding Types Example}
235\endlist
236
237*/
238
239/*!
240\example declarative/cppextensions/referenceexamples/signal
241\title Extending QML - Signal Support Example
242
243This example builds on:
244\list
245\o \l {Extending QML - Attached Properties Example}
246\o \l {Extending QML - Grouped Properties Example}
247\o \l {Extending QML - Default Property Example}
248\o \l {Extending QML - Inheritance and Coercion Example}
249\o \l {Extending QML - Object and List Property Types Example}
250\o \l {Extending QML - Adding Types Example}
251\endlist
252
253*/
254
255/*!
256\example declarative/cppextensions/referenceexamples/methods
257\title Extending QML - Methods Example
258
259This example builds on:
260\list
261\o \l {Extending QML - Default Property Example}
262\o \l {Extending QML - Inheritance and Coercion Example}
263\o \l {Extending QML - Object and List Property Types Example}
264\o \l {Extending QML - Adding Types Example}
265\endlist
266
267*/
268
269/*!
270\example declarative/cppextensions/referenceexamples/valuesource
271\title Extending QML - Property Value Source Example
272
273This example builds on:
274\list
275\o \l {Extending QML - Signal Support Example}
276\o \l {Extending QML - Attached Properties Example}
277\o \l {Extending QML - Grouped Properties Example}
278\o \l {Extending QML - Default Property Example}
279\o \l {Extending QML - Inheritance and Coercion Example}
280\o \l {Extending QML - Object and List Property Types Example}
281\o \l {Extending QML - Adding Types Example}
282\endlist
283
284*/
285
286/*!
287\example declarative/cppextensions/referenceexamples/binding
288\title Extending QML - Binding Example
289
290This example builds on:
291\list
292\o \l {Extending QML - Property Value Source Example}
293\o \l {Extending QML - Signal Support Example}
294\o \l {Extending QML - Attached Properties Example}
295\o \l {Extending QML - Grouped Properties Example}
296\o \l {Extending QML - Default Property Example}
297\o \l {Extending QML - Inheritance and Coercion Example}
298\o \l {Extending QML - Object and List Property Types Example}
299\o \l {Extending QML - Adding Types Example}
300\endlist
301
302*/
Note: See TracBrowser for help on using the repository browser.