| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** All rights reserved. | 
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com) | 
|---|
| 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 qt-info@nokia.com. | 
|---|
| 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 | 
|---|
| 140 | previous example.  However, as we have repurposed the People class as a common | 
|---|
| 141 | base for Boy and Girl, we want to prevent it from being instantiated from QML | 
|---|
| 142 | directly - an explicit Boy or Girl should be instantiated instead. | 
|---|
| 143 |  | 
|---|
| 144 | \snippet examples/declarative/cppextensions/referenceexamples/coercion/main.cpp 0 | 
|---|
| 145 |  | 
|---|
| 146 | While we want to disallow instantiating Person from within QML, it still needs | 
|---|
| 147 | to be registered with the QML engine, so that it can be used as a property type | 
|---|
| 148 | and other types can be coerced to it. | 
|---|
| 149 |  | 
|---|
| 150 | \section2 Define Boy and Girl | 
|---|
| 151 |  | 
|---|
| 152 | The implementation of Boy and Girl are trivial. | 
|---|
| 153 |  | 
|---|
| 154 | \snippet examples/declarative/cppextensions/referenceexamples/coercion/person.cpp 1 | 
|---|
| 155 |  | 
|---|
| 156 | All that is necessary is to implement the constructor, and to register the types | 
|---|
| 157 | and their QML name with the QML engine. | 
|---|
| 158 |  | 
|---|
| 159 | \section1 Running the example | 
|---|
| 160 |  | 
|---|
| 161 | The BirthdayParty element has not changed since the previous example.  The | 
|---|
| 162 | celebrant and guests property still use the People type. | 
|---|
| 163 |  | 
|---|
| 164 | \snippet examples/declarative/cppextensions/referenceexamples/coercion/birthdayparty.h 0 | 
|---|
| 165 |  | 
|---|
| 166 | However, as all three types, Person, Boy and Girl, have been registered with the | 
|---|
| 167 | QML system, on assignment QML automatically (and type-safely) converts the Boy | 
|---|
| 168 | and Girl objects into a Person. | 
|---|
| 169 |  | 
|---|
| 170 | The main.cpp file in the example includes a simple shell application that | 
|---|
| 171 | loads 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 |  | 
|---|
| 178 | This 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 |  | 
|---|
| 185 | The Default Property Example is a minor modification of the | 
|---|
| 186 | \l {Extending QML - Inheritance and Coercion Example} that simplifies the | 
|---|
| 187 | specification 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 |  | 
|---|
| 193 | The 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 |  | 
|---|
| 198 | The default property specifies the property to assign to whenever an explicit | 
|---|
| 199 | property is not specified, in the case of the BirthdayParty element the guest | 
|---|
| 200 | property.  It is purely a syntactic simplification, the behavior is identical | 
|---|
| 201 | to specifying the property by name, but it can add a more natural feel in many | 
|---|
| 202 | situations.  The default property must be either an object or list property. | 
|---|
| 203 |  | 
|---|
| 204 | \section1 Running the example | 
|---|
| 205 |  | 
|---|
| 206 | The main.cpp file in the example includes a simple shell application that | 
|---|
| 207 | loads 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 |  | 
|---|
| 214 | This 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 |  | 
|---|
| 228 | This 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 |  | 
|---|
| 243 | This 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 |  | 
|---|
| 259 | This 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 |  | 
|---|
| 273 | This 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 |  | 
|---|
| 290 | This 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 | */ | 
|---|