diff options
author | Friedemann Kleint <[email protected]> | 2021-11-02 09:04:21 +0100 |
---|---|---|
committer | Friedemann Kleint <[email protected]> | 2021-11-04 10:51:39 +0100 |
commit | c442ed4d65b0319e4abd7a931cfb2f1f72842b84 (patch) | |
tree | 99009060ae1c23ee98bef51f213926cd961f468a /examples/qml/referenceexamples/signal | |
parent | 33b16814c90fb4b70043be4f65fbbf783aee5d86 (diff) |
Polish the QML reference examples
- Use member initialization, which allows for using
constructors from the base classes
- Use qsizetype for indexes
- Use qInfo() instead of qWarning() for printing
- Add spaces/fix formatting
Pick-to: 6.2
Change-Id: Iebce1b810ce00f29395207d93303363b3b71e52e
Reviewed-by: Cristian Maureira-Fredes <[email protected]>
Reviewed-by: Ulf Hermann <[email protected]>
Diffstat (limited to 'examples/qml/referenceexamples/signal')
-rw-r--r-- | examples/qml/referenceexamples/signal/birthdayparty.cpp | 20 | ||||
-rw-r--r-- | examples/qml/referenceexamples/signal/birthdayparty.h | 10 | ||||
-rw-r--r-- | examples/qml/referenceexamples/signal/main.cpp | 12 | ||||
-rw-r--r-- | examples/qml/referenceexamples/signal/person.cpp | 26 | ||||
-rw-r--r-- | examples/qml/referenceexamples/signal/person.h | 16 |
5 files changed, 27 insertions, 57 deletions
diff --git a/examples/qml/referenceexamples/signal/birthdayparty.cpp b/examples/qml/referenceexamples/signal/birthdayparty.cpp index 215c09782a..27498c9cc9 100644 --- a/examples/qml/referenceexamples/signal/birthdayparty.cpp +++ b/examples/qml/referenceexamples/signal/birthdayparty.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2017 The Qt Company Ltd. +** Copyright (C) 2021 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the examples of the Qt Toolkit. @@ -47,16 +47,12 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ -#include "birthdayparty.h" -BirthdayPartyAttached::BirthdayPartyAttached(QObject *object) -: QObject(object) -{ -} +#include "birthdayparty.h" QDate BirthdayPartyAttached::rsvp() const { - return m_rsvp; + return m_rsvp; } void BirthdayPartyAttached::setRsvp(QDate d) @@ -64,12 +60,6 @@ void BirthdayPartyAttached::setRsvp(QDate d) m_rsvp = d; } - -BirthdayParty::BirthdayParty(QObject *parent) -: QObject(parent), m_host(nullptr) -{ -} - Person *BirthdayParty::host() const { return m_host; @@ -85,12 +75,12 @@ QQmlListProperty<Person> BirthdayParty::guests() return {this, &m_guests}; } -int BirthdayParty::guestCount() const +qsizetype BirthdayParty::guestCount() const { return m_guests.count(); } -Person *BirthdayParty::guest(int index) const +Person *BirthdayParty::guest(qsizetype index) const { return m_guests.at(index); } diff --git a/examples/qml/referenceexamples/signal/birthdayparty.h b/examples/qml/referenceexamples/signal/birthdayparty.h index 6d7e022df0..c4a4da5dfb 100644 --- a/examples/qml/referenceexamples/signal/birthdayparty.h +++ b/examples/qml/referenceexamples/signal/birthdayparty.h @@ -61,7 +61,7 @@ class BirthdayPartyAttached : public QObject Q_PROPERTY(QDate rsvp READ rsvp WRITE setRsvp) QML_ANONYMOUS public: - BirthdayPartyAttached(QObject *object); + using QObject::QObject; QDate rsvp() const; void setRsvp(QDate); @@ -79,14 +79,14 @@ class BirthdayParty : public QObject QML_ELEMENT QML_ATTACHED(BirthdayPartyAttached) public: - BirthdayParty(QObject *parent = nullptr); + using QObject::QObject; Person *host() const; void setHost(Person *); QQmlListProperty<Person> guests(); - int guestCount() const; - Person *guest(int) const; + qsizetype guestCount() const; + Person *guest(qsizetype) const; static BirthdayPartyAttached *qmlAttachedProperties(QObject *); @@ -97,7 +97,7 @@ signals: // ![0] private: - Person *m_host; + Person *m_host = nullptr; QList<Person *> m_guests; }; diff --git a/examples/qml/referenceexamples/signal/main.cpp b/examples/qml/referenceexamples/signal/main.cpp index 7ef3595a8c..d0b060d8e6 100644 --- a/examples/qml/referenceexamples/signal/main.cpp +++ b/examples/qml/referenceexamples/signal/main.cpp @@ -63,14 +63,14 @@ int main(int argc, char ** argv) auto *party = qobject_cast<BirthdayParty *>(component.create()); if (party && party->host()) { - qWarning() << party->host()->name() << "is having a birthday!"; + qInfo() << party->host()->name() << "is having a birthday!"; if (qobject_cast<Boy *>(party->host())) - qWarning() << "He is inviting:"; + qInfo() << "He is inviting:"; else - qWarning() << "She is inviting:"; + qInfo() << "She is inviting:"; - for (int ii = 0; ii < party->guestCount(); ++ii) { + for (qsizetype ii = 0; ii < party->guestCount(); ++ii) { Person *guest = party->guest(ii); QDate rsvpDate; @@ -80,9 +80,9 @@ int main(int argc, char ** argv) rsvpDate = attached->property("rsvp").toDate(); if (rsvpDate.isNull()) - qWarning() << " " << guest->name() << "RSVP date: Hasn't RSVP'd"; + qInfo() << " " << guest->name() << "RSVP date: Hasn't RSVP'd"; else - qWarning() << " " << guest->name() << "RSVP date:" << qPrintable(rsvpDate.toString()); + qInfo() << " " << guest->name() << "RSVP date:" << rsvpDate.toString(); } party->startParty(); diff --git a/examples/qml/referenceexamples/signal/person.cpp b/examples/qml/referenceexamples/signal/person.cpp index 0603644108..4e52da76c8 100644 --- a/examples/qml/referenceexamples/signal/person.cpp +++ b/examples/qml/referenceexamples/signal/person.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2017 The Qt Company Ltd. +** Copyright (C) 2021 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the examples of the Qt Toolkit. @@ -47,12 +47,8 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ -#include "person.h" -ShoeDescription::ShoeDescription(QObject *parent) -: QObject(parent), m_size(0), m_price(0) -{ -} +#include "person.h" int ShoeDescription::size() const { @@ -94,11 +90,6 @@ void ShoeDescription::setPrice(qreal p) m_price = p; } -Person::Person(QObject *parent) -: QObject(parent) -{ -} - QString Person::name() const { return m_name; @@ -113,16 +104,3 @@ ShoeDescription *Person::shoe() { return &m_shoe; } - - -Boy::Boy(QObject * parent) -: Person(parent) -{ -} - - -Girl::Girl(QObject * parent) -: Person(parent) -{ -} - diff --git a/examples/qml/referenceexamples/signal/person.h b/examples/qml/referenceexamples/signal/person.h index 7283f39f61..ddd3baa5f2 100644 --- a/examples/qml/referenceexamples/signal/person.h +++ b/examples/qml/referenceexamples/signal/person.h @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2017 The Qt Company Ltd. +** Copyright (C) 2021 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the examples of the Qt Toolkit. @@ -47,6 +47,7 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ + #ifndef PERSON_H #define PERSON_H @@ -63,7 +64,7 @@ class ShoeDescription : public QObject Q_PROPERTY(qreal price READ price WRITE setPrice) QML_ANONYMOUS public: - ShoeDescription(QObject *parent = nullptr); + using QObject::QObject; int size() const; void setSize(int); @@ -76,11 +77,12 @@ public: qreal price() const; void setPrice(qreal); + private: - int m_size; + int m_size = 0; QColor m_color; QString m_brand; - qreal m_price; + qreal m_price = 0; }; class Person : public QObject @@ -90,7 +92,7 @@ class Person : public QObject Q_PROPERTY(ShoeDescription *shoe READ shoe) QML_ANONYMOUS public: - Person(QObject *parent = nullptr); + using QObject::QObject; QString name() const; void setName(const QString &); @@ -106,7 +108,7 @@ class Boy : public Person Q_OBJECT QML_ELEMENT public: - Boy(QObject * parent = nullptr); + using Person::Person; }; class Girl : public Person @@ -114,7 +116,7 @@ class Girl : public Person Q_OBJECT QML_ELEMENT public: - Girl(QObject * parent = nullptr); + using Person::Person; }; #endif // PERSON_H |