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.
BirthdayParty { host: Person { name: "Bob Jones" shoeSize: 12 } guests: [ Person { name: "Leo Hodges" }, Person { name: "Jack Smith" }, Person { name: "Anne Brown" } ] }
Declare the BirthdayParty
The BirthdayParty class is declared like this:
class BirthdayParty : public QObject { Q_OBJECT Q_PROPERTY(Person *host READ host WRITE setHost) Q_PROPERTY(QQmlListProperty<Person> guests READ guests) public: BirthdayParty(QObject *parent = 0); Person *host() const; void setHost(Person *); QQmlListProperty<Person> guests(); void appendGuest(Person*); int guestCount() const; Person *guest(int) const; void clearGuests(); private: static void appendGuest(QQmlListProperty<Person>*, Person*); static int guestCount(QQmlListProperty<Person>*); static Person* guest(QQmlListProperty<Person>*, int); static void clearGuests(QQmlListProperty<Person>*); Person *m_host; QVector<Person *> m_guests; };
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<T>. 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.
Person *BirthdayParty::host() const { return m_host; } void BirthdayParty::setHost(Person *c) { m_host = c; } QQmlListProperty<Person> BirthdayParty::guests() { return QQmlListProperty<Person>(this, this, &BirthdayParty::appendGuest, &BirthdayParty::guestCount, &BirthdayParty::guest, &BirthdayParty::clearGuests); } void BirthdayParty::appendGuest(Person* p) { m_guests.append(p); } int BirthdayParty::guestCount() const { return m_guests.count(); } Person *BirthdayParty::guest(int index) const { return m_guests.at(index); } void BirthdayParty::clearGuests() { m_guests.clear(); }
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.
Files:
- referenceexamples/properties/birthdayparty.cpp
- referenceexamples/properties/birthdayparty.h
- referenceexamples/properties/example.qml
- referenceexamples/properties/main.cpp
- referenceexamples/properties/person.cpp
- referenceexamples/properties/person.h
- referenceexamples/properties/properties.pro
- referenceexamples/properties/properties.qrc
© 2021 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.