diff options
author | Michael Winkelmann <Michael.winkelmann@qt.io> | 2017-03-02 17:16:09 +0100 |
---|---|---|
committer | Simon Hausmann <simon.hausmann@qt.io> | 2017-03-07 07:11:44 +0000 |
commit | a54f08e1ead70e821f438279e548d54e38c4aa8f (patch) | |
tree | b63f726da6bbac502a4da5ed754463e31fcbf2cf /examples | |
parent | ba6de61acd3129ad1435f6bca7f564385212f95c (diff) |
Replace QList<Person*> with QVector<Person*> and function pointers
The usage of the QQmlListProperty QList constructor is discouraged since
QList violates QML's memory management rules.
Replaced the QList with a QVector and passed the function pointers to the
QQmlListProperty constructor instead, as officially recommended.
Change-Id: I6d28a43530cc3edd5e7d89c351bad70deb721689
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'examples')
-rw-r--r-- | examples/qml/referenceexamples/properties/birthdayparty.cpp | 31 | ||||
-rw-r--r-- | examples/qml/referenceexamples/properties/birthdayparty.h | 10 |
2 files changed, 39 insertions, 2 deletions
diff --git a/examples/qml/referenceexamples/properties/birthdayparty.cpp b/examples/qml/referenceexamples/properties/birthdayparty.cpp index b69b7c8a11..0b426fc00b 100644 --- a/examples/qml/referenceexamples/properties/birthdayparty.cpp +++ b/examples/qml/referenceexamples/properties/birthdayparty.cpp @@ -57,9 +57,18 @@ void BirthdayParty::setHost(Person *c) QQmlListProperty<Person> BirthdayParty::guests() { - return QQmlListProperty<Person>(this, m_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(); @@ -69,5 +78,25 @@ Person *BirthdayParty::guest(int index) const { return m_guests.at(index); } + +void BirthdayParty::clearGuests() { + return m_guests.clear(); +} + // ![0] +void BirthdayParty::appendGuest(QQmlListProperty<Person>* list, Person* p) { + reinterpret_cast< BirthdayParty* >(list->data)->appendGuest(p); +} + +void BirthdayParty::clearGuests(QQmlListProperty<Person>* list) { + reinterpret_cast< BirthdayParty* >(list->data)->clearGuests(); +} + +Person* BirthdayParty::guest(QQmlListProperty<Person>* list, int i) { + return reinterpret_cast< BirthdayParty* >(list->data)->guest(i); +} + +int BirthdayParty::guestCount(QQmlListProperty<Person>* list) { + return reinterpret_cast< BirthdayParty* >(list->data)->guestCount(); +} diff --git a/examples/qml/referenceexamples/properties/birthdayparty.h b/examples/qml/referenceexamples/properties/birthdayparty.h index d0a2cad285..df55df3e80 100644 --- a/examples/qml/referenceexamples/properties/birthdayparty.h +++ b/examples/qml/referenceexamples/properties/birthdayparty.h @@ -41,6 +41,7 @@ #define BIRTHDAYPARTY_H #include <QObject> +#include <QVector> #include <QQmlListProperty> #include "person.h" @@ -63,12 +64,19 @@ public: 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; - QList<Person *> m_guests; + QVector<Person *> m_guests; }; // ![3] |