diff options
author | Aaron Kennedy <aaron.kennedy@nokia.com> | 2010-02-22 04:56:49 (GMT) |
---|---|---|
committer | Aaron Kennedy <aaron.kennedy@nokia.com> | 2010-02-22 05:53:27 (GMT) |
commit | 33eb76f050b45718d87926a8ff7afc89d6201c16 (patch) | |
tree | 935ddc2337b3891aab1ad7edcb06a62aabf14668 /examples/declarative | |
parent | 5f63321e3fe2b436d469d2722dc464ea4c7830c4 (diff) | |
download | Qt-33eb76f050b45718d87926a8ff7afc89d6201c16.zip Qt-33eb76f050b45718d87926a8ff7afc89d6201c16.tar.gz Qt-33eb76f050b45718d87926a8ff7afc89d6201c16.tar.bz2 |
Replace QmlList* and QList* support with a single QmlListProperty type
As a value type QmlListProperty doesn't consume any memory in the object.
It also has a companion QmlListReference class that is part of the public
API for C++ developers to interact with that also manages memory issues
that existed with previous solutions (if the containing QObject was
destroyed it left a dangling pointer).
Diffstat (limited to 'examples/declarative')
25 files changed, 156 insertions, 57 deletions
diff --git a/examples/declarative/extending/attached/birthdayparty.cpp b/examples/declarative/extending/attached/birthdayparty.cpp index 9dc13de..ffdda57 100644 --- a/examples/declarative/extending/attached/birthdayparty.cpp +++ b/examples/declarative/extending/attached/birthdayparty.cpp @@ -72,9 +72,19 @@ void BirthdayParty::setCelebrant(Person *c) m_celebrant = c; } -QmlList<Person *> *BirthdayParty::guests() +QmlListProperty<Person> BirthdayParty::guests() { - return &m_guests; + return QmlListProperty<Person>(this, m_guests); +} + +int BirthdayParty::guestCount() const +{ + return m_guests.count(); +} + +Person *BirthdayParty::guest(int index) const +{ + return m_guests.at(index); } BirthdayPartyAttached *BirthdayParty::qmlAttachedProperties(QObject *object) diff --git a/examples/declarative/extending/attached/birthdayparty.h b/examples/declarative/extending/attached/birthdayparty.h index bd8952b..9ba0f8b 100644 --- a/examples/declarative/extending/attached/birthdayparty.h +++ b/examples/declarative/extending/attached/birthdayparty.h @@ -65,7 +65,7 @@ class BirthdayParty : public QObject { Q_OBJECT Q_PROPERTY(Person *celebrant READ celebrant WRITE setCelebrant) -Q_PROPERTY(QmlList<Person *> *guests READ guests) +Q_PROPERTY(QmlListProperty<Person> guests READ guests) Q_CLASSINFO("DefaultProperty", "guests") public: BirthdayParty(QObject *parent = 0); @@ -73,12 +73,14 @@ public: Person *celebrant() const; void setCelebrant(Person *); - QmlList<Person *> *guests(); + QmlListProperty<Person> guests(); + int guestCount() const; + Person *guest(int) const; static BirthdayPartyAttached *qmlAttachedProperties(QObject *); private: Person *m_celebrant; - QmlConcreteList<Person *> m_guests; + QList<Person *> m_guests; }; QML_DECLARE_TYPEINFO(BirthdayParty, QML_HAS_ATTACHED_PROPERTIES) diff --git a/examples/declarative/extending/attached/main.cpp b/examples/declarative/extending/attached/main.cpp index 2ec783f..27a9287 100644 --- a/examples/declarative/extending/attached/main.cpp +++ b/examples/declarative/extending/attached/main.cpp @@ -61,8 +61,8 @@ int main(int argc, char ** argv) else qWarning() << "She is inviting:"; - for (int ii = 0; ii < party->guests()->count(); ++ii) { - Person *guest = party->guests()->at(ii); + for (int ii = 0; ii < party->guestCount(); ++ii) { + Person *guest = party->guest(ii); QDate rsvpDate; QObject *attached = diff --git a/examples/declarative/extending/binding/birthdayparty.cpp b/examples/declarative/extending/binding/birthdayparty.cpp index 8a409af..62b9c7b 100644 --- a/examples/declarative/extending/binding/birthdayparty.cpp +++ b/examples/declarative/extending/binding/birthdayparty.cpp @@ -77,9 +77,19 @@ void BirthdayParty::setCelebrant(Person *c) emit celebrantChanged(); } -QmlList<Person *> *BirthdayParty::guests() +QmlListProperty<Person> BirthdayParty::guests() { - return &m_guests; + return QmlListProperty<Person>(this, m_guests); +} + +int BirthdayParty::guestCount() const +{ + return m_guests.count(); +} + +Person *BirthdayParty::guest(int index) const +{ + return m_guests.at(index); } void BirthdayParty::startParty() diff --git a/examples/declarative/extending/binding/birthdayparty.h b/examples/declarative/extending/binding/birthdayparty.h index 5651c65..8bdb76a 100644 --- a/examples/declarative/extending/binding/birthdayparty.h +++ b/examples/declarative/extending/binding/birthdayparty.h @@ -71,7 +71,7 @@ Q_OBJECT // ![0] Q_PROPERTY(Person *celebrant READ celebrant WRITE setCelebrant NOTIFY celebrantChanged) // ![0] -Q_PROPERTY(QmlList<Person *> *guests READ guests) +Q_PROPERTY(QmlListProperty<Person> guests READ guests) Q_PROPERTY(QString speaker READ speaker WRITE setSpeaker) Q_CLASSINFO("DefaultProperty", "guests") public: @@ -80,7 +80,9 @@ public: Person *celebrant() const; void setCelebrant(Person *); - QmlList<Person *> *guests(); + QmlListProperty<Person> guests(); + int guestCount() const; + Person *guest(int) const; QString speaker() const; void setSpeaker(const QString &); @@ -94,7 +96,7 @@ signals: private: Person *m_celebrant; - QmlConcreteList<Person *> m_guests; + QList<Person *> m_guests; }; QML_DECLARE_TYPEINFO(BirthdayParty, QML_HAS_ATTACHED_PROPERTIES) diff --git a/examples/declarative/extending/binding/main.cpp b/examples/declarative/extending/binding/main.cpp index 4ad9929..ba38e82 100644 --- a/examples/declarative/extending/binding/main.cpp +++ b/examples/declarative/extending/binding/main.cpp @@ -61,8 +61,8 @@ int main(int argc, char ** argv) else qWarning() << "She is inviting:"; - for (int ii = 0; ii < party->guests()->count(); ++ii) { - Person *guest = party->guests()->at(ii); + for (int ii = 0; ii < party->guestCount(); ++ii) { + Person *guest = party->guest(ii); QDate rsvpDate; QObject *attached = diff --git a/examples/declarative/extending/coercion/birthdayparty.cpp b/examples/declarative/extending/coercion/birthdayparty.cpp index 014d307..15a4ca9 100644 --- a/examples/declarative/extending/coercion/birthdayparty.cpp +++ b/examples/declarative/extending/coercion/birthdayparty.cpp @@ -55,9 +55,19 @@ void BirthdayParty::setCelebrant(Person *c) m_celebrant = c; } -QmlList<Person *> *BirthdayParty::guests() +QmlListProperty<Person> BirthdayParty::guests() { - return &m_guests; + return QmlListProperty<Person>(this, m_guests); +} + +int BirthdayParty::guestCount() const +{ + return m_guests.count(); +} + +Person *BirthdayParty::guest(int index) const +{ + return m_guests.at(index); } QML_DEFINE_TYPE(People, 1,0, BirthdayParty, BirthdayParty); diff --git a/examples/declarative/extending/coercion/birthdayparty.h b/examples/declarative/extending/coercion/birthdayparty.h index 8563ec3..5a9eb08 100644 --- a/examples/declarative/extending/coercion/birthdayparty.h +++ b/examples/declarative/extending/coercion/birthdayparty.h @@ -50,7 +50,7 @@ class BirthdayParty : public QObject Q_OBJECT // ![0] Q_PROPERTY(Person *celebrant READ celebrant WRITE setCelebrant) -Q_PROPERTY(QmlList<Person *> *guests READ guests) +Q_PROPERTY(QmlListProperty<Person> guests READ guests) // ![0] public: BirthdayParty(QObject *parent = 0); @@ -58,11 +58,13 @@ public: Person *celebrant() const; void setCelebrant(Person *); - QmlList<Person *> *guests(); + QmlListProperty<Person> guests(); + int guestCount() const; + Person *guest(int) const; private: Person *m_celebrant; - QmlConcreteList<Person *> m_guests; + QList<Person *> m_guests; }; QML_DECLARE_TYPE(BirthdayParty); diff --git a/examples/declarative/extending/coercion/main.cpp b/examples/declarative/extending/coercion/main.cpp index c6cc847..ccbee83 100644 --- a/examples/declarative/extending/coercion/main.cpp +++ b/examples/declarative/extending/coercion/main.cpp @@ -60,8 +60,9 @@ int main(int argc, char ** argv) qWarning() << "He is inviting:"; else qWarning() << "She is inviting:"; - for (int ii = 0; ii < party->guests()->count(); ++ii) - qWarning() << " " << party->guests()->at(ii)->name(); + + for (int ii = 0; ii < party->guestCount(); ++ii) + qWarning() << " " << party->guest(ii)->name(); } else { qWarning() << "An error occured"; } diff --git a/examples/declarative/extending/default/birthdayparty.cpp b/examples/declarative/extending/default/birthdayparty.cpp index 014d307..15a4ca9 100644 --- a/examples/declarative/extending/default/birthdayparty.cpp +++ b/examples/declarative/extending/default/birthdayparty.cpp @@ -55,9 +55,19 @@ void BirthdayParty::setCelebrant(Person *c) m_celebrant = c; } -QmlList<Person *> *BirthdayParty::guests() +QmlListProperty<Person> BirthdayParty::guests() { - return &m_guests; + return QmlListProperty<Person>(this, m_guests); +} + +int BirthdayParty::guestCount() const +{ + return m_guests.count(); +} + +Person *BirthdayParty::guest(int index) const +{ + return m_guests.at(index); } QML_DEFINE_TYPE(People, 1,0, BirthdayParty, BirthdayParty); diff --git a/examples/declarative/extending/default/birthdayparty.h b/examples/declarative/extending/default/birthdayparty.h index 869b32c..f25f8c2 100644 --- a/examples/declarative/extending/default/birthdayparty.h +++ b/examples/declarative/extending/default/birthdayparty.h @@ -50,7 +50,7 @@ class BirthdayParty : public QObject { Q_OBJECT Q_PROPERTY(Person *celebrant READ celebrant WRITE setCelebrant) -Q_PROPERTY(QmlList<Person *> *guests READ guests) +Q_PROPERTY(QmlListProperty<Person> guests READ guests) Q_CLASSINFO("DefaultProperty", "guests") public: BirthdayParty(QObject *parent = 0); @@ -58,11 +58,13 @@ public: Person *celebrant() const; void setCelebrant(Person *); - QmlList<Person *> *guests(); + QmlListProperty<Person> guests(); + int guestCount() const; + Person *guest(int) const; private: Person *m_celebrant; - QmlConcreteList<Person *> m_guests; + QList<Person *> m_guests; }; // ![0] QML_DECLARE_TYPE(BirthdayParty); diff --git a/examples/declarative/extending/default/main.cpp b/examples/declarative/extending/default/main.cpp index c6cc847..ccbee83 100644 --- a/examples/declarative/extending/default/main.cpp +++ b/examples/declarative/extending/default/main.cpp @@ -60,8 +60,9 @@ int main(int argc, char ** argv) qWarning() << "He is inviting:"; else qWarning() << "She is inviting:"; - for (int ii = 0; ii < party->guests()->count(); ++ii) - qWarning() << " " << party->guests()->at(ii)->name(); + + for (int ii = 0; ii < party->guestCount(); ++ii) + qWarning() << " " << party->guest(ii)->name(); } else { qWarning() << "An error occured"; } diff --git a/examples/declarative/extending/grouped/birthdayparty.cpp b/examples/declarative/extending/grouped/birthdayparty.cpp index 014d307..15a4ca9 100644 --- a/examples/declarative/extending/grouped/birthdayparty.cpp +++ b/examples/declarative/extending/grouped/birthdayparty.cpp @@ -55,9 +55,19 @@ void BirthdayParty::setCelebrant(Person *c) m_celebrant = c; } -QmlList<Person *> *BirthdayParty::guests() +QmlListProperty<Person> BirthdayParty::guests() { - return &m_guests; + return QmlListProperty<Person>(this, m_guests); +} + +int BirthdayParty::guestCount() const +{ + return m_guests.count(); +} + +Person *BirthdayParty::guest(int index) const +{ + return m_guests.at(index); } QML_DEFINE_TYPE(People, 1,0, BirthdayParty, BirthdayParty); diff --git a/examples/declarative/extending/grouped/birthdayparty.h b/examples/declarative/extending/grouped/birthdayparty.h index 3f4a3a6..fd0d955 100644 --- a/examples/declarative/extending/grouped/birthdayparty.h +++ b/examples/declarative/extending/grouped/birthdayparty.h @@ -49,7 +49,7 @@ class BirthdayParty : public QObject { Q_OBJECT Q_PROPERTY(Person *celebrant READ celebrant WRITE setCelebrant) -Q_PROPERTY(QmlList<Person *> *guests READ guests) +Q_PROPERTY(QmlListProperty<Person> guests READ guests) Q_CLASSINFO("DefaultProperty", "guests") public: BirthdayParty(QObject *parent = 0); @@ -57,11 +57,13 @@ public: Person *celebrant() const; void setCelebrant(Person *); - QmlList<Person *> *guests(); + QmlListProperty<Person> guests(); + int guestCount() const; + Person *guest(int) const; private: Person *m_celebrant; - QmlConcreteList<Person *> m_guests; + QList<Person *> m_guests; }; QML_DECLARE_TYPE(BirthdayParty); diff --git a/examples/declarative/extending/grouped/main.cpp b/examples/declarative/extending/grouped/main.cpp index 23ba8bf..79aaab5 100644 --- a/examples/declarative/extending/grouped/main.cpp +++ b/examples/declarative/extending/grouped/main.cpp @@ -62,8 +62,8 @@ int main(int argc, char ** argv) qWarning() << "She is inviting:"; Person *bestShoe = 0; - for (int ii = 0; ii < party->guests()->count(); ++ii) { - Person *guest = party->guests()->at(ii); + for (int ii = 0; ii < party->guestCount(); ++ii) { + Person *guest = party->guest(ii); qWarning() << " " << guest->name(); if (!bestShoe || bestShoe->shoe()->price() < guest->shoe()->price()) diff --git a/examples/declarative/extending/properties/birthdayparty.cpp b/examples/declarative/extending/properties/birthdayparty.cpp index 332b090..23e6e58 100644 --- a/examples/declarative/extending/properties/birthdayparty.cpp +++ b/examples/declarative/extending/properties/birthdayparty.cpp @@ -56,9 +56,19 @@ void BirthdayParty::setCelebrant(Person *c) m_celebrant = c; } -QmlList<Person *> *BirthdayParty::guests() +QmlListProperty<Person> BirthdayParty::guests() { - return &m_guests; + return QmlListProperty<Person>(this, m_guests); +} + +int BirthdayParty::guestCount() const +{ + return m_guests.count(); +} + +Person *BirthdayParty::guest(int index) const +{ + return m_guests.at(index); } // ![0] diff --git a/examples/declarative/extending/properties/birthdayparty.h b/examples/declarative/extending/properties/birthdayparty.h index ceefd5b..e5d316c 100644 --- a/examples/declarative/extending/properties/birthdayparty.h +++ b/examples/declarative/extending/properties/birthdayparty.h @@ -54,7 +54,7 @@ Q_OBJECT Q_PROPERTY(Person *celebrant READ celebrant WRITE setCelebrant) // ![1] // ![2] -Q_PROPERTY(QmlList<Person *> *guests READ guests) +Q_PROPERTY(QmlListProperty<Person> guests READ guests) // ![2] // ![3] public: @@ -63,11 +63,13 @@ public: Person *celebrant() const; void setCelebrant(Person *); - QmlList<Person *> *guests(); + QmlListProperty<Person> guests(); + int guestCount() const; + Person *guest(int) const; private: Person *m_celebrant; - QmlConcreteList<Person *> m_guests; + QList<Person *> m_guests; }; QML_DECLARE_TYPE(BirthdayParty); // ![3] diff --git a/examples/declarative/extending/properties/main.cpp b/examples/declarative/extending/properties/main.cpp index 229e59a..97d7905 100644 --- a/examples/declarative/extending/properties/main.cpp +++ b/examples/declarative/extending/properties/main.cpp @@ -56,8 +56,8 @@ int main(int argc, char ** argv) if (party && party->celebrant()) { qWarning() << party->celebrant()->name() << "is having a birthday!"; qWarning() << "They are inviting:"; - for (int ii = 0; ii < party->guests()->count(); ++ii) - qWarning() << " " << party->guests()->at(ii)->name(); + for (int ii = 0; ii < party->guestCount(); ++ii) + qWarning() << " " << party->guest(ii)->name(); } else { qWarning() << "An error occured"; } diff --git a/examples/declarative/extending/signal/birthdayparty.cpp b/examples/declarative/extending/signal/birthdayparty.cpp index 88c5459..d8686f0 100644 --- a/examples/declarative/extending/signal/birthdayparty.cpp +++ b/examples/declarative/extending/signal/birthdayparty.cpp @@ -72,9 +72,19 @@ void BirthdayParty::setCelebrant(Person *c) m_celebrant = c; } -QmlList<Person *> *BirthdayParty::guests() +QmlListProperty<Person> BirthdayParty::guests() { - return &m_guests; + return QmlListProperty<Person>(this, m_guests); +} + +int BirthdayParty::guestCount() const +{ + return m_guests.count(); +} + +Person *BirthdayParty::guest(int index) const +{ + return m_guests.at(index); } void BirthdayParty::startParty() diff --git a/examples/declarative/extending/signal/birthdayparty.h b/examples/declarative/extending/signal/birthdayparty.h index 8ce5d7b..30ed43b 100644 --- a/examples/declarative/extending/signal/birthdayparty.h +++ b/examples/declarative/extending/signal/birthdayparty.h @@ -65,7 +65,7 @@ class BirthdayParty : public QObject { Q_OBJECT Q_PROPERTY(Person *celebrant READ celebrant WRITE setCelebrant) -Q_PROPERTY(QmlList<Person *> *guests READ guests) +Q_PROPERTY(QmlListProperty<Person> guests READ guests) Q_CLASSINFO("DefaultProperty", "guests") public: BirthdayParty(QObject *parent = 0); @@ -73,7 +73,9 @@ public: Person *celebrant() const; void setCelebrant(Person *); - QmlList<Person *> *guests(); + QmlListProperty<Person> guests(); + int guestCount() const; + Person *guest(int) const; static BirthdayPartyAttached *qmlAttachedProperties(QObject *); @@ -85,7 +87,7 @@ signals: private: Person *m_celebrant; - QmlConcreteList<Person *> m_guests; + QList<Person *> m_guests; }; QML_DECLARE_TYPEINFO(BirthdayParty, QML_HAS_ATTACHED_PROPERTIES) diff --git a/examples/declarative/extending/signal/main.cpp b/examples/declarative/extending/signal/main.cpp index 4e981c5..eb3bb4b 100644 --- a/examples/declarative/extending/signal/main.cpp +++ b/examples/declarative/extending/signal/main.cpp @@ -61,8 +61,8 @@ int main(int argc, char ** argv) else qWarning() << "She is inviting:"; - for (int ii = 0; ii < party->guests()->count(); ++ii) { - Person *guest = party->guests()->at(ii); + for (int ii = 0; ii < party->guestCount(); ++ii) { + Person *guest = party->guest(ii); QDate rsvpDate; QObject *attached = diff --git a/examples/declarative/extending/valuesource/birthdayparty.cpp b/examples/declarative/extending/valuesource/birthdayparty.cpp index b483f68..a5b3fab 100644 --- a/examples/declarative/extending/valuesource/birthdayparty.cpp +++ b/examples/declarative/extending/valuesource/birthdayparty.cpp @@ -72,9 +72,19 @@ void BirthdayParty::setCelebrant(Person *c) m_celebrant = c; } -QmlList<Person *> *BirthdayParty::guests() +QmlListProperty<Person> BirthdayParty::guests() { - return &m_guests; + return QmlListProperty<Person>(this, m_guests); +} + +int BirthdayParty::guestCount() const +{ + return m_guests.count(); +} + +Person *BirthdayParty::guest(int index) const +{ + return m_guests.at(index); } void BirthdayParty::startParty() diff --git a/examples/declarative/extending/valuesource/birthdayparty.h b/examples/declarative/extending/valuesource/birthdayparty.h index e7ca461..b5a0522 100644 --- a/examples/declarative/extending/valuesource/birthdayparty.h +++ b/examples/declarative/extending/valuesource/birthdayparty.h @@ -66,7 +66,7 @@ class BirthdayParty : public QObject { Q_OBJECT Q_PROPERTY(Person *celebrant READ celebrant WRITE setCelebrant) -Q_PROPERTY(QmlList<Person *> *guests READ guests) +Q_PROPERTY(QmlListProperty<Person> guests READ guests) // ![0] Q_PROPERTY(QString speaker READ speaker WRITE setSpeaker) // ![0] @@ -77,7 +77,10 @@ public: Person *celebrant() const; void setCelebrant(Person *); - QmlList<Person *> *guests(); + QmlListProperty<Person> guests(); + int guestCount() const; + Person *guest(int) const; + QString speaker() const; void setSpeaker(const QString &); @@ -90,7 +93,7 @@ signals: private: Person *m_celebrant; - QmlConcreteList<Person *> m_guests; + QList<Person *> m_guests; }; QML_DECLARE_TYPEINFO(BirthdayParty, QML_HAS_ATTACHED_PROPERTIES) diff --git a/examples/declarative/extending/valuesource/main.cpp b/examples/declarative/extending/valuesource/main.cpp index 4ad9929..ba38e82 100644 --- a/examples/declarative/extending/valuesource/main.cpp +++ b/examples/declarative/extending/valuesource/main.cpp @@ -61,8 +61,8 @@ int main(int argc, char ** argv) else qWarning() << "She is inviting:"; - for (int ii = 0; ii < party->guests()->count(); ++ii) { - Person *guest = party->guests()->at(ii); + for (int ii = 0; ii < party->guestCount(); ++ii) { + Person *guest = party->guest(ii); QDate rsvpDate; QObject *attached = diff --git a/examples/declarative/objectlistmodel/main.cpp b/examples/declarative/objectlistmodel/main.cpp index 5eb6b7d..9e38bea 100644 --- a/examples/declarative/objectlistmodel/main.cpp +++ b/examples/declarative/objectlistmodel/main.cpp @@ -68,7 +68,7 @@ int main(int argc, char ** argv) dataList.append(new DataObject("Item 4", "yellow")); QmlContext *ctxt = view.rootContext(); - ctxt->setContextProperty("myModel", QVariant::fromValue(&dataList)); + ctxt->setContextProperty("myModel", QVariant::fromValue(dataList)); view.execute(); view.show(); |