Extending QML - Object and List Property Types Example¶
Exporting C++ Properties.
This example builds on:
The Object and List Property Types example shows how to add object and list properties in QML. This example adds a BirthdayParty type 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.
<Code snippet "referenceexamples/properties/example.qml:0" not found>
Declare the BirthdayParty¶
The BirthdayParty class is declared like this:
<Code snippet "referenceexamples/properties/birthdayparty.h:0" not found> <Code snippet "referenceexamples/properties/birthdayparty.h:1" not found> <Code snippet "referenceexamples/properties/birthdayparty.h:2" not found> <Code snippet "referenceexamples/properties/birthdayparty.h:3" not found>
The class contains a member to store the celebrant object, and also a
QList <Person *> member.
In QML, the type of a list properties - and the guests property is a list of people - are all of type
QQmlListProperty is simple value type that contains a set of function pointers. QML calls these function pointers whenever it needs to read from, write to or otherwise interact with the list. In addition to concrete lists like the people list used in this example, the use of
QQmlListProperty allows for “virtual lists” and other advanced scenarios.
Define the BirthdayParty¶
The implementation of BirthdayParty property accessors is straight forward.
<Code snippet "referenceexamples/properties/birthdayparty.cpp:0" not found>
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.
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.