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 |
|
---|
32 | The Adding Types Example shows how to add a new element type, \c Person, to QML.
|
---|
33 | The \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 |
|
---|
39 | All QML elements map to C++ types. Here we declare a basic C++ Person class
|
---|
40 | with the two properties we want accessible on the QML type - name and shoeSize.
|
---|
41 | Although in this example we use the same name for the C++ class as the QML
|
---|
42 | element, 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 |
|
---|
50 | The Person class implementation is quite basic. The property accessors simply
|
---|
51 | return members of the object instance.
|
---|
52 |
|
---|
53 | The \c main.cpp file also calls the \c qmlRegisterType() function to
|
---|
54 | register the \c Person type with QML as a type in the People library version 1.0,
|
---|
55 | and defines the mapping between the C++ and QML class names.
|
---|
56 |
|
---|
57 | \section1 Running the example
|
---|
58 |
|
---|
59 | The main.cpp file in the example includes a simple shell application that
|
---|
60 | loads 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 |
|
---|
67 | This example builds on:
|
---|
68 | \list
|
---|
69 | \o \l {Extending QML - Adding Types Example}
|
---|
70 | \endlist
|
---|
71 |
|
---|
72 | The Object and List Property Types example shows how to add object and list
|
---|
73 | properties in QML. This example adds a BirthdayParty element that specifies
|
---|
74 | a birthday party, consisting of a celebrant and a list of guests. People are
|
---|
75 | specified 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 |
|
---|
81 | The 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 |
|
---|
88 | The class contains a member to store the celebrant object, and also a
|
---|
89 | QList<Person *> member.
|
---|
90 |
|
---|
91 | In QML, the type of a list properties - and the guests property is a list of
|
---|
92 | people - are all of type QDeclarativeListProperty<T>. QDeclarativeListProperty is simple value
|
---|
93 | type that contains a set of function pointers. QML calls these function
|
---|
94 | pointers whenever it needs to read from, write to or otherwise interact with
|
---|
95 | the list. In addition to concrete lists like the people list used in this
|
---|
96 | example, the use of QDeclarativeListProperty allows for "virtual lists" and other advanced
|
---|
97 | scenarios.
|
---|
98 |
|
---|
99 | \section2 Define the BirthdayParty
|
---|
100 |
|
---|
101 | The 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 |
|
---|
107 | The main.cpp file in the example includes a simple shell application that
|
---|
108 | loads 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 |
|
---|
115 | This 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 |
|
---|
121 | The Inheritance and Coercion Example shows how to use base classes to assign
|
---|
122 | elements of more than one type to a property. It specializes the Person element
|
---|
123 | developed 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 |
|
---|
131 | The Person class remains unaltered in this example and the Boy and Girl C++
|
---|
132 | classes are trivial extensions of it. As an example, the inheritance used here
|
---|
133 | is a little contrived, but in real applications it is likely that the two
|
---|
134 | extensions would add additional properties or modify the Person classes
|
---|
135 | behavior.
|
---|
136 |
|
---|
137 | \section2 Define People as a base class
|
---|
138 |
|
---|
139 | The implementation of the People class itself has not changed since the the
|
---|
|
---|