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