summaryrefslogtreecommitdiffstats
path: root/doc/src/declarative/extending-examples.qdoc
blob: c569ee04fc84a71a1f231c138ed654fdf9ecba3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

/*!
\example declarative/extending/adding
\title Extending QML - Adding Types Example

The Adding Types Example shows how to add a new element type, \c Person, to QML.
The \c Person type can be used from QML like this:

\snippet examples/declarative/extending/adding/example.qml 0

\section1 Declare the Person class

All QML elements map to C++ types.  Here we declare a basic C++ Person class 
with the two properties we want accessible on the QML type - name and shoeSize.
Although in this example we use the same name for the C++ class as the QML 
element, the C++ class can be named differently, or appear in a namespace.

\snippet examples/declarative/extending/adding/person.h 0

Following the class declaration, we include the QML_DECLARE_TYPE() macro.  This
is necessary to declare the type to QML.  It also includes the logic necessary
to expose the class to Qt's meta system - that is, it includes the 
Q_DECLARE_METATYPE() functionality.

\section1 Define the Person class

\snippet examples/declarative/extending/adding/person.cpp 0

The Person class implementation is quite basic.  The property accessors simply
return members of the object instance.

The implementation must also include the QML_DEFINE_TYPE() macro.  This macro
registers the Person class with QML as a type in the People library version 1.0,
and defines the mapping between the C++ and QML class names.

\section1 Running the example

The main.cpp file in the example includes a simple shell application that 
loads and runs the QML snippet shown at the beginning of this page.  
*/

/*!
\example declarative/extending/properties
\title Extending QML - Object and List Property Types Example

This example builds on:
\list
\o \l {Extending QML - Adding Types Example}
\endlist

The Object and List Property Types example shows how to add object and list
properties in QML.  This example adds a BirthdayParty element that specifies
a birthday party, consisting of a celebrant and a list of guests.  People are
specified using the People QML type built in the previous example.

\snippet examples/declarative/extending/properties/example.qml 0

\section1 Declare the BirthdayParty

The BirthdayParty class is declared like this:

\snippet examples/declarative/extending/properties/birthdayparty.h 0
\snippet examples/declarative/extending/properties/birthdayparty.h 1
\snippet examples/declarative/extending/properties/birthdayparty.h 2
\snippet examples/declarative/extending/properties/birthdayparty.h 3

The class contains a member to store the celebrant object, and also a 
QmlConcreteList<Person *> member.  

In QML, the type of a list properties - and the guests property is a list of 
people - are all of type QmlList<T *>*.  QmlList is an abstract list interface
that allows a developer to react to QML accessing and modifying the contents of
the list.  This is useful for implementing "virtual lists" or other advanced
scenarios, but can't be used directly for the common case of just wanting a
regular list of things.  For this a concrete implementation, QmlConcreteList, is
provided and that is used here.

\section2 Define the BirthdayParty

The implementation of BirthdayParty property accessors is straight forward.

\snippet examples/declarative/extending/properties/birthdayparty.cpp 0

\section1 Running the example

The main.cpp file in the example includes a simple shell application that 
loads and runs the QML snippet shown at the beginning of this page.  
*/

/*!
\example declarative/extending/coercion
\title Extending QML - Inheritance and Coercion Example

This example builds on:
\list
\o \l {Extending QML - Object and List Property Types Example}
\o \l {Extending QML - Adding Types Example}
\endlist

The Inheritance and Coercion Example shows how to use base classes to assign
elements of more than one type to a property.  It specializes the Person element
developed in the previous examples into two elements - a \c Boy and a \c Girl.

\snippet examples/declarative/extending/coercion/example.qml 0

\section1 Declare Boy and Girl

\snippet examples/declarative/extending/coercion/person.h 0

The Person class remains unaltered in this example and the Boy and Girl C++ 
classes are trivial extensions of it.  As an example, the inheritance used here
is a little contrived, but in real applications it is likely that the two
extensions would add additional properties or modify the Person classes 
behavior.

\section2 Define People as a base class

The implementation of the People class itself has not changed since the the 
previous example.  However, as we have repurposed the People class as a common
base for Boy and Girl, we want to prevent it from being instantiated from QML
directly - an explicit Boy or Girl should be instantiated instead.

\snippet examples/declarative/extending/coercion/person.cpp 0

While we want to disallow instantiating Person from within QML, it still needs
to be registered with the QML engine, so that it can be used as a property type
and other types can be coerced to it.  To register a type, without defining a
named mapping into QML, we use the QML_DEFINE_NOCREATE_TYPE() macro instead of
the QML_DEFINE_TYPE() macro used previously.

\section2 Define Boy and Girl

The implementation of Boy and Girl are trivial.

\snippet examples/declarative/extending/coercion/person.cpp 1

All that is necessary is to implement the constructor, and to register the types
and their QML name with the QML engine.

\section1 Running the example

The BirthdayParty element has not changed since the previous example.  The 
celebrant and guests property still use the People type.  

\snippet examples/declarative/extending/coercion/birthdayparty.h 0

However, as all three types, Person, Boy and Girl, have been registered with the
QML system, on assignment QML automatically (and type-safely) converts the Boy
and Girl objects into a Person.

The main.cpp file in the example includes a simple shell application that 
loads and runs the QML snippet shown at the beginning of this page.  
*/

/*!
\example declarative/extending/default
\title Extending QML - Default Property Example

This example builds on:
\list
\o \l {Extending QML - Inheritance and Coercion Example}
\o \l {Extending QML - Object and List Property Types Example}
\o \l {Extending QML - Adding Types Example}
\endlist

The Default Property Example is a minor modification of the 
\l {Extending QML - Inheritance and Coercion Example} that simplifies the 
specification of a BirthdayParty through the use of a default property.

\snippet examples/declarative/extending/default/example.qml 0

\section1 Declaring the BirthdayParty class

The only difference between this example and the last, is the addition of the
\c DefaultProperty class info annotation.  

\snippet examples/declarative/extending/default/birthdayparty.h 0

The default property specifies the property to assign to whenever an explicit 
property is not specified, in the case of the BirthdayParty element the guest
property.  It is purely a syntactic simplification, the behavior is identical 
to specifying the property by name, but it can add a more natural feel in many
situations.  The default property must be either an object or list property.

\section1 Running the example

The main.cpp file in the example includes a simple shell application that 
loads and runs the QML snippet shown at the beginning of this page.  
*/

/*!
\example declarative/extending/grouped
\title Extending QML - Grouped Properties Example

This example builds on:
\list
\o \l {Extending QML - Default Property Example}
\o \l {Extending QML - Inheritance and Coercion Example}
\o \l {Extending QML - Object and List Property Types Example}
\o \l {Extending QML - Adding Types Example}
\endlist

*/

/*!
\example declarative/extending/grouped
\title Extending QML - Attached Properties Example

This example builds on:
\list
\o \l {Extending QML - Grouped Properties Example}
\o \l {Extending QML - Default Property Example}
\o \l {Extending QML - Inheritance and Coercion Example}
\o \l {Extending QML - Object and List Property Types Example}
\o \l {Extending QML - Adding Types Example}
\endlist

*/

/*!
\example declarative/extending/signal
\title Extending QML - Signal Support Example

This example builds on:
\list
\o \l {Extending QML - Attached Properties Example}
\o \l {Extending QML - Grouped Properties Example}
\o \l {Extending QML - Default Property Example}
\o \l {Extending QML - Inheritance and Coercion Example}
\o \l {Extending QML - Object and List Property Types Example}
\o \l {Extending QML - Adding Types Example}
\endlist

*/

/*!
\example declarative/extending/valuesource
\title Extending QML - Property Value Source Example

This example builds on:
\list
\o \l {Extending QML - Signal Support Example}
\o \l {Extending QML - Attached Properties Example}
\o \l {Extending QML - Grouped Properties Example}
\o \l {Extending QML - Default Property Example}
\o \l {Extending QML - Inheritance and Coercion Example}
\o \l {Extending QML - Object and List Property Types Example}
\o \l {Extending QML - Adding Types Example}
\endlist

*/

/*!
\example declarative/extending/binding
\title Extending QML - Binding Example

This example builds on:
\list
\o \l {Extending QML - Property Value Source Example}
\o \l {Extending QML - Signal Support Example}
\o \l {Extending QML - Attached Properties Example}
\o \l {Extending QML - Grouped Properties Example}
\o \l {Extending QML - Default Property Example}
\o \l {Extending QML - Inheritance and Coercion Example}
\o \l {Extending QML - Object and List Property Types Example}
\o \l {Extending QML - Adding Types Example}
\endlist

*/