From a54f08e1ead70e821f438279e548d54e38c4aa8f Mon Sep 17 00:00:00 2001 From: Michael Winkelmann Date: Thu, 2 Mar 2017 17:16:09 +0100 Subject: Replace QList with QVector 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 --- .../referenceexamples/properties/birthdayparty.cpp | 31 +++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'examples/qml/referenceexamples/properties/birthdayparty.cpp') 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 BirthdayParty::guests() { - return QQmlListProperty(this, m_guests); + return QQmlListProperty(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* list, Person* p) { + reinterpret_cast< BirthdayParty* >(list->data)->appendGuest(p); +} + +void BirthdayParty::clearGuests(QQmlListProperty* list) { + reinterpret_cast< BirthdayParty* >(list->data)->clearGuests(); +} + +Person* BirthdayParty::guest(QQmlListProperty* list, int i) { + return reinterpret_cast< BirthdayParty* >(list->data)->guest(i); +} + +int BirthdayParty::guestCount(QQmlListProperty* list) { + return reinterpret_cast< BirthdayParty* >(list->data)->guestCount(); +} -- cgit v1.2.3