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" } ] }
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) QML_ELEMENT public: using QObject::QObject; Person *host() const; void setHost(Person *); QQmlListProperty<Person> guests(); void appendGuest(Person *); qsizetype guestCount() const; Person *guest(qsizetype) const; void clearGuests(); void replaceGuest(qsizetype, Person *); void removeLastGuest(); private: static void appendGuest(QQmlListProperty<Person> *, Person *); static qsizetype guestCount(QQmlListProperty<Person> *); static Person* guest(QQmlListProperty<Person> *, qsizetype); static void clearGuests(QQmlListProperty<Person> *); static void replaceGuest(QQmlListProperty<Person> *, qsizetype, Person *); static void removeLastGuest(QQmlListProperty<Person> *); Person *m_host = nullptr; QList<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.
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 {this, this, &BirthdayParty::appendGuest, &BirthdayParty::guestCount, &BirthdayParty::guest, &BirthdayParty::clearGuests, &BirthdayParty::replaceGuest, &BirthdayParty::removeLastGuest}; } void BirthdayParty::appendGuest(Person *p) { m_guests.append(p); } qsizetype BirthdayParty::guestCount() const { return m_guests.count(); } Person *BirthdayParty::guest(qsizetype index) const { return m_guests.at(index); } void BirthdayParty::clearGuests() { m_guests.clear(); } void BirthdayParty::replaceGuest(qsizetype index, Person *p) { m_guests[index] = p; } void BirthdayParty::removeLastGuest() { m_guests.removeLast(); }
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.
As part of the free Business evaluation, we offer a free welcome call for companies, to talk about your requirements, and how the Felgo SDK & Services can help you. Just sign up and schedule your call.
Sign up now to start your free Business evaluation: