From c442ed4d65b0319e4abd7a931cfb2f1f72842b84 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Tue, 2 Nov 2021 09:04:21 +0100 Subject: 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 Reviewed-by: Ulf Hermann --- examples/qml/doc/src/qml-extending.qdoc | 19 +++++---------- examples/qml/referenceexamples/adding/main.cpp | 12 ++++++---- examples/qml/referenceexamples/adding/person.cpp | 8 ++----- examples/qml/referenceexamples/adding/person.h | 7 +++--- .../referenceexamples/attached/birthdayparty.cpp | 17 ++++--------- .../qml/referenceexamples/attached/birthdayparty.h | 13 +++++----- examples/qml/referenceexamples/attached/main.cpp | 15 ++++++------ examples/qml/referenceexamples/attached/person.cpp | 26 ++------------------ examples/qml/referenceexamples/attached/person.h | 16 +++++++------ .../referenceexamples/binding/birthdayparty.cpp | 20 ++++------------ .../qml/referenceexamples/binding/birthdayparty.h | 13 +++++----- .../binding/happybirthdaysong.cpp | 5 ++-- .../referenceexamples/binding/happybirthdaysong.h | 4 ++-- examples/qml/referenceexamples/binding/main.cpp | 12 +++++----- examples/qml/referenceexamples/binding/person.cpp | 26 ++------------------ examples/qml/referenceexamples/binding/person.h | 17 +++++++------ .../referenceexamples/coercion/birthdayparty.cpp | 13 ++++------ .../qml/referenceexamples/coercion/birthdayparty.h | 11 +++++---- examples/qml/referenceexamples/coercion/main.cpp | 13 +++++----- examples/qml/referenceexamples/coercion/person.cpp | 22 ++--------------- examples/qml/referenceexamples/coercion/person.h | 12 ++++++---- .../referenceexamples/default/birthdayparty.cpp | 12 ++++------ .../qml/referenceexamples/default/birthdayparty.h | 11 +++++---- examples/qml/referenceexamples/default/main.cpp | 10 ++++---- examples/qml/referenceexamples/default/person.cpp | 21 ++-------------- examples/qml/referenceexamples/default/person.h | 11 +++++---- .../referenceexamples/grouped/birthdayparty.cpp | 13 ++++------ .../qml/referenceexamples/grouped/birthdayparty.h | 9 +++---- examples/qml/referenceexamples/grouped/main.cpp | 12 +++++----- examples/qml/referenceexamples/grouped/person.cpp | 26 ++------------------ examples/qml/referenceexamples/grouped/person.h | 16 +++++++------ .../referenceexamples/methods/birthdayparty.cpp | 12 ++++------ .../qml/referenceexamples/methods/birthdayparty.h | 11 +++++---- examples/qml/referenceexamples/methods/main.cpp | 11 +++++---- examples/qml/referenceexamples/methods/person.cpp | 9 ++----- examples/qml/referenceexamples/methods/person.h | 8 ++++--- .../referenceexamples/properties/birthdayparty.cpp | 28 +++++++++++----------- .../referenceexamples/properties/birthdayparty.h | 23 +++++++++--------- examples/qml/referenceexamples/properties/main.cpp | 8 +++---- .../qml/referenceexamples/properties/person.cpp | 9 ++----- examples/qml/referenceexamples/properties/person.h | 8 ++++--- .../qml/referenceexamples/signal/birthdayparty.cpp | 20 ++++------------ .../qml/referenceexamples/signal/birthdayparty.h | 10 ++++---- examples/qml/referenceexamples/signal/main.cpp | 12 +++++----- examples/qml/referenceexamples/signal/person.cpp | 26 ++------------------ examples/qml/referenceexamples/signal/person.h | 16 +++++++------ .../valuesource/birthdayparty.cpp | 21 ++++------------ .../referenceexamples/valuesource/birthdayparty.h | 14 ++++++----- .../valuesource/happybirthdaysong.cpp | 8 +++---- .../valuesource/happybirthdaysong.h | 7 +++--- .../qml/referenceexamples/valuesource/main.cpp | 12 +++++----- .../qml/referenceexamples/valuesource/person.cpp | 26 ++------------------ .../qml/referenceexamples/valuesource/person.h | 18 ++++++++------ 53 files changed, 285 insertions(+), 474 deletions(-) (limited to 'examples/qml') diff --git a/examples/qml/doc/src/qml-extending.qdoc b/examples/qml/doc/src/qml-extending.qdoc index c9922ebd45..310436759e 100644 --- a/examples/qml/doc/src/qml-extending.qdoc +++ b/examples/qml/doc/src/qml-extending.qdoc @@ -168,10 +168,12 @@ developed in the previous examples into two types - a \c Boy and a \c Girl. \snippet referenceexamples/coercion/person.h 0 The Person class remains unaltered in this example and the Boy and Girl C++ -classes are trivial extensions of it. As an example, the inheritance used here -is a little contrived, but in real applications it is likely that the two -extensions would add additional properties or modify the Person classes -behavior. +classes are trivial extensions of it. The types and their QML name are +registered with the QML engine. + +As an example, the inheritance used here is a little contrived, but in real +applications it is likely that the two extensions would add additional +properties or modify the Person classes behavior. \section2 Define People as a Base Class @@ -187,15 +189,6 @@ to be registered with the QML engine, so that it can be used as a property type and other types can be coerced to it. This is what the QML_UNCREATABLE macro does. -\section2 Define Boy and Girl - -The implementation of Boy and Girl is trivial. - -\snippet referenceexamples/coercion/person.cpp 1 - -All that is necessary is to implement the constructor, and to register the types -and their QML name with the QML engine. - \section1 Running the Example The BirthdayParty type has not changed since the previous example. The diff --git a/examples/qml/referenceexamples/adding/main.cpp b/examples/qml/referenceexamples/adding/main.cpp index 6c3920f04b..fbf3e71af7 100644 --- a/examples/qml/referenceexamples/adding/main.cpp +++ b/examples/qml/referenceexamples/adding/main.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,6 +47,7 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ + #include #include #include @@ -60,12 +61,13 @@ int main(int argc, char ** argv) QQmlEngine engine; QQmlComponent component(&engine, QUrl("qrc:example.qml")); auto *person = qobject_cast(component.create()); - if (person) { - qWarning() << "The person's name is" << person->name(); - qWarning() << "They wear a" << person->shoeSize() << "sized shoe"; - } else { + if (!person) { qWarning() << component.errors(); + return EXIT_FAILURE; } + qInfo() << "The person's name is" << person->name() + << "\nThey wear a" << person->shoeSize() << "sized shoe"; + return EXIT_SUCCESS; } diff --git a/examples/qml/referenceexamples/adding/person.cpp b/examples/qml/referenceexamples/adding/person.cpp index de4a33dd00..4741442cf0 100644 --- a/examples/qml/referenceexamples/adding/person.cpp +++ b/examples/qml/referenceexamples/adding/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,14 +47,10 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ + #include "person.h" // ![0] -Person::Person(QObject *parent) -: QObject(parent), m_shoeSize(0) -{ -} - QString Person::name() const { return m_name; diff --git a/examples/qml/referenceexamples/adding/person.h b/examples/qml/referenceexamples/adding/person.h index 530c335dee..03a72d184f 100644 --- a/examples/qml/referenceexamples/adding/person.h +++ b/examples/qml/referenceexamples/adding/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 @@ -61,7 +62,7 @@ class Person : public QObject Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize) QML_ELEMENT public: - Person(QObject *parent = nullptr); + using QObject::QObject; QString name() const; void setName(const QString &); @@ -71,7 +72,7 @@ public: private: QString m_name; - int m_shoeSize; + int m_shoeSize = 0; }; //![0] diff --git a/examples/qml/referenceexamples/attached/birthdayparty.cpp b/examples/qml/referenceexamples/attached/birthdayparty.cpp index 9aed5df621..fc27a22631 100644 --- a/examples/qml/referenceexamples/attached/birthdayparty.cpp +++ b/examples/qml/referenceexamples/attached/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,12 +47,8 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ -#include "birthdayparty.h" -BirthdayPartyAttached::BirthdayPartyAttached(QObject *object) -: QObject(object) -{ -} +#include "birthdayparty.h" QDate BirthdayPartyAttached::rsvp() const { @@ -64,11 +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; @@ -84,12 +75,12 @@ QQmlListProperty 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/attached/birthdayparty.h b/examples/qml/referenceexamples/attached/birthdayparty.h index 4f2ed1702a..30f5cbbb64 100644 --- a/examples/qml/referenceexamples/attached/birthdayparty.h +++ b/examples/qml/referenceexamples/attached/birthdayparty.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 BIRTHDAYPARTY_H #define BIRTHDAYPARTY_H @@ -61,7 +62,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); @@ -83,20 +84,20 @@ class BirthdayParty : public QObject //! [declare attached] public: - BirthdayParty(QObject *parent = nullptr); + using QObject::QObject; Person *host() const; void setHost(Person *); QQmlListProperty guests(); - int guestCount() const; - Person *guest(int) const; + qsizetype guestCount() const; + Person *guest(qsizetype) const; //! [static attached] static BirthdayPartyAttached *qmlAttachedProperties(QObject *); //! [static attached] private: - Person *m_host; + Person *m_host = nullptr; QList m_guests; }; diff --git a/examples/qml/referenceexamples/attached/main.cpp b/examples/qml/referenceexamples/attached/main.cpp index 2fceb98ac9..5f8497bed3 100644 --- a/examples/qml/referenceexamples/attached/main.cpp +++ b/examples/qml/referenceexamples/attached/main.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,6 +47,7 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ + #include #include #include @@ -63,14 +64,14 @@ int main(int argc, char ** argv) auto *party = qobject_cast(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(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); //! [query rsvp] @@ -81,9 +82,9 @@ int main(int argc, char ** argv) rsvpDate = attached->property("rsvp").toDate(); //! [query rsvp] 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(); } return EXIT_SUCCESS; diff --git a/examples/qml/referenceexamples/attached/person.cpp b/examples/qml/referenceexamples/attached/person.cpp index 0603644108..4e52da76c8 100644 --- a/examples/qml/referenceexamples/attached/person.cpp +++ b/examples/qml/referenceexamples/attached/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/attached/person.h b/examples/qml/referenceexamples/attached/person.h index 7283f39f61..ddd3baa5f2 100644 --- a/examples/qml/referenceexamples/attached/person.h +++ b/examples/qml/referenceexamples/attached/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 diff --git a/examples/qml/referenceexamples/binding/birthdayparty.cpp b/examples/qml/referenceexamples/binding/birthdayparty.cpp index 02a1adbb35..ce86c7faa4 100644 --- a/examples/qml/referenceexamples/binding/birthdayparty.cpp +++ b/examples/qml/referenceexamples/binding/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,12 +47,8 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ -#include "birthdayparty.h" -BirthdayPartyAttached::BirthdayPartyAttached(QObject *object) -: QObject(object) -{ -} +#include "birthdayparty.h" QDate BirthdayPartyAttached::rsvp() const { @@ -67,12 +63,6 @@ void BirthdayPartyAttached::setRsvp(QDate d) } } - -BirthdayParty::BirthdayParty(QObject *parent) -: QObject(parent), m_host(nullptr) -{ -} - Person *BirthdayParty::host() const { return m_host; @@ -90,12 +80,12 @@ QQmlListProperty BirthdayParty::guests() return QQmlListProperty(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); } @@ -113,7 +103,7 @@ QString BirthdayParty::announcement() const void BirthdayParty::setAnnouncement(const QString &speak) { - qWarning() << qPrintable(speak); + qWarning().noquote() << speak; } BirthdayPartyAttached *BirthdayParty::qmlAttachedProperties(QObject *object) diff --git a/examples/qml/referenceexamples/binding/birthdayparty.h b/examples/qml/referenceexamples/binding/birthdayparty.h index 846a016cfc..2881dfead2 100644 --- a/examples/qml/referenceexamples/binding/birthdayparty.h +++ b/examples/qml/referenceexamples/binding/birthdayparty.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 BIRTHDAYPARTY_H #define BIRTHDAYPARTY_H @@ -62,7 +63,7 @@ class BirthdayPartyAttached : public QObject Q_PROPERTY(QDate rsvp READ rsvp WRITE setRsvp NOTIFY rsvpChanged) QML_ANONYMOUS public: - BirthdayPartyAttached(QObject *object); + using QObject::QObject; QDate rsvp() const; void setRsvp(QDate); @@ -86,14 +87,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 guests(); - int guestCount() const; - Person *guest(int) const; + qsizetype guestCount() const; + Person *guest(qsizetype) const; QString announcement() const; void setAnnouncement(const QString &); @@ -106,7 +107,7 @@ signals: void hostChanged(); private: - Person *m_host; + Person *m_host = nullptr; QList m_guests; }; diff --git a/examples/qml/referenceexamples/binding/happybirthdaysong.cpp b/examples/qml/referenceexamples/binding/happybirthdaysong.cpp index 5f8e6d696e..2722b65e06 100644 --- a/examples/qml/referenceexamples/binding/happybirthdaysong.cpp +++ b/examples/qml/referenceexamples/binding/happybirthdaysong.cpp @@ -50,10 +50,9 @@ #include "happybirthdaysong.h" #include -HappyBirthdaySong::HappyBirthdaySong(QObject *parent) -: QObject(parent), m_line(-1) +HappyBirthdaySong::HappyBirthdaySong(QObject *parent) : + QObject(parent) { - setName(QString()); auto *timer = new QTimer(this); QObject::connect(timer, &QTimer::timeout, this, &HappyBirthdaySong::advance); timer->start(1000); diff --git a/examples/qml/referenceexamples/binding/happybirthdaysong.h b/examples/qml/referenceexamples/binding/happybirthdaysong.h index c84e9d9e5e..078bdac72b 100644 --- a/examples/qml/referenceexamples/binding/happybirthdaysong.h +++ b/examples/qml/referenceexamples/binding/happybirthdaysong.h @@ -64,7 +64,7 @@ class HappyBirthdaySong : public QObject, public QQmlPropertyValueSource Q_INTERFACES(QQmlPropertyValueSource) QML_ELEMENT public: - HappyBirthdaySong(QObject *parent = nullptr); + explicit HappyBirthdaySong(QObject *parent = nullptr); void setTarget(const QQmlProperty &) override; @@ -77,7 +77,7 @@ private slots: signals: void nameChanged(); private: - int m_line; + qsizetype m_line = -1; QStringList m_lyrics; QQmlProperty m_target; QString m_name; diff --git a/examples/qml/referenceexamples/binding/main.cpp b/examples/qml/referenceexamples/binding/main.cpp index 2e9995fcd8..a100497659 100644 --- a/examples/qml/referenceexamples/binding/main.cpp +++ b/examples/qml/referenceexamples/binding/main.cpp @@ -64,14 +64,14 @@ int main(int argc, char ** argv) auto *party = qobject_cast(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(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; @@ -81,9 +81,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/binding/person.cpp b/examples/qml/referenceexamples/binding/person.cpp index 1a23d0a4f5..0296b0a7c6 100644 --- a/examples/qml/referenceexamples/binding/person.cpp +++ b/examples/qml/referenceexamples/binding/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 { @@ -110,11 +106,6 @@ void ShoeDescription::setPrice(qreal p) emit shoeChanged(); } -Person::Person(QObject *parent) -: QObject(parent) -{ -} - QString Person::name() const { return m_name; @@ -133,16 +124,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/binding/person.h b/examples/qml/referenceexamples/binding/person.h index af9864950a..e84075bb36 100644 --- a/examples/qml/referenceexamples/binding/person.h +++ b/examples/qml/referenceexamples/binding/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 NOTIFY shoeChanged) QML_ANONYMOUS public: - ShoeDescription(QObject *parent = nullptr); + using QObject::QObject; int size() const; void setSize(int); @@ -76,14 +77,15 @@ public: qreal price() const; void setPrice(qreal); + signals: void shoeChanged(); 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 @@ -95,12 +97,13 @@ class Person : public QObject // ![0] QML_ANONYMOUS public: - Person(QObject *parent = nullptr); + using QObject::QObject; QString name() const; void setName(const QString &); ShoeDescription *shoe(); + signals: void nameChanged(); @@ -114,7 +117,7 @@ class Boy : public Person Q_OBJECT QML_ELEMENT public: - Boy(QObject * parent = nullptr); + using Person::Person; }; class Girl : public Person @@ -122,7 +125,7 @@ class Girl : public Person Q_OBJECT QML_ELEMENT public: - Girl(QObject * parent = nullptr); + using Person::Person; }; #endif // PERSON_H diff --git a/examples/qml/referenceexamples/coercion/birthdayparty.cpp b/examples/qml/referenceexamples/coercion/birthdayparty.cpp index 81db8ab1b8..269f45e02c 100644 --- a/examples/qml/referenceexamples/coercion/birthdayparty.cpp +++ b/examples/qml/referenceexamples/coercion/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,12 +47,8 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ -#include "birthdayparty.h" -BirthdayParty::BirthdayParty(QObject *parent) -: QObject(parent), m_host(nullptr) -{ -} +#include "birthdayparty.h" Person *BirthdayParty::host() const { @@ -69,13 +65,12 @@ QQmlListProperty 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/coercion/birthdayparty.h b/examples/qml/referenceexamples/coercion/birthdayparty.h index ba8b2445f1..a504479e4d 100644 --- a/examples/qml/referenceexamples/coercion/birthdayparty.h +++ b/examples/qml/referenceexamples/coercion/birthdayparty.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 BIRTHDAYPARTY_H #define BIRTHDAYPARTY_H @@ -63,17 +64,17 @@ class BirthdayParty : public QObject QML_ELEMENT // ![0] public: - BirthdayParty(QObject *parent = nullptr); + using QObject::QObject; Person *host() const; void setHost(Person *); QQmlListProperty guests(); - int guestCount() const; - Person *guest(int) const; + qsizetype guestCount() const; + Person *guest(qsizetype) const; private: - Person *m_host; + Person *m_host = nullptr; QList m_guests; }; diff --git a/examples/qml/referenceexamples/coercion/main.cpp b/examples/qml/referenceexamples/coercion/main.cpp index 6b47d0d4c4..095602af9d 100644 --- a/examples/qml/referenceexamples/coercion/main.cpp +++ b/examples/qml/referenceexamples/coercion/main.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,6 +47,7 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ + #include #include #include @@ -63,15 +64,15 @@ int main(int argc, char ** argv) auto *party = qobject_cast(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(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) - qWarning() << " " << party->guest(ii)->name(); + for (qsizetype ii = 0; ii < party->guestCount(); ++ii) + qInfo() << " " << party->guest(ii)->name(); return EXIT_SUCCESS; } diff --git a/examples/qml/referenceexamples/coercion/person.cpp b/examples/qml/referenceexamples/coercion/person.cpp index 178d7052ce..e2847a2682 100644 --- a/examples/qml/referenceexamples/coercion/person.cpp +++ b/examples/qml/referenceexamples/coercion/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" -Person::Person(QObject *parent) -: QObject(parent), m_shoeSize(0) -{ -} +#include "person.h" QString Person::name() const { @@ -73,17 +69,3 @@ void Person::setShoeSize(int s) { m_shoeSize = s; } - -// ![1] -Boy::Boy(QObject * parent) -: Person(parent) -{ -} - - -Girl::Girl(QObject * parent) -: Person(parent) -{ -} - -// ![1] diff --git a/examples/qml/referenceexamples/coercion/person.h b/examples/qml/referenceexamples/coercion/person.h index 7e2828da04..1ec095e841 100644 --- a/examples/qml/referenceexamples/coercion/person.h +++ b/examples/qml/referenceexamples/coercion/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,16 +64,17 @@ class Person : public QObject QML_UNCREATABLE("Person is an abstract base class.") //![0] public: - Person(QObject *parent = nullptr); + using QObject::QObject; QString name() const; void setName(const QString &); int shoeSize() const; void setShoeSize(int); + private: QString m_name; - int m_shoeSize; + int m_shoeSize = 0; }; @@ -82,7 +84,7 @@ class Boy : public Person Q_OBJECT QML_ELEMENT public: - Boy(QObject * parent = nullptr); + using Person::Person; }; //! [girl class] @@ -91,7 +93,7 @@ class Girl : public Person Q_OBJECT QML_ELEMENT public: - Girl(QObject * parent = nullptr); + using Person::Person; }; //! [girl class] diff --git a/examples/qml/referenceexamples/default/birthdayparty.cpp b/examples/qml/referenceexamples/default/birthdayparty.cpp index 81db8ab1b8..aeaf6fbd9c 100644 --- a/examples/qml/referenceexamples/default/birthdayparty.cpp +++ b/examples/qml/referenceexamples/default/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,12 +47,8 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ -#include "birthdayparty.h" -BirthdayParty::BirthdayParty(QObject *parent) -: QObject(parent), m_host(nullptr) -{ -} +#include "birthdayparty.h" Person *BirthdayParty::host() const { @@ -69,12 +65,12 @@ QQmlListProperty 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/default/birthdayparty.h b/examples/qml/referenceexamples/default/birthdayparty.h index 5eb6c88763..cb12a1510b 100644 --- a/examples/qml/referenceexamples/default/birthdayparty.h +++ b/examples/qml/referenceexamples/default/birthdayparty.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 BIRTHDAYPARTY_H #define BIRTHDAYPARTY_H @@ -63,17 +64,17 @@ class BirthdayParty : public QObject Q_CLASSINFO("DefaultProperty", "guests") QML_ELEMENT public: - BirthdayParty(QObject *parent = nullptr); + using QObject::QObject; Person *host() const; void setHost(Person *); QQmlListProperty guests(); - int guestCount() const; - Person *guest(int) const; + qsizetype guestCount() const; + Person *guest(qsizetype) const; private: - Person *m_host; + Person *m_host = nullptr; QList m_guests; }; // ![0] diff --git a/examples/qml/referenceexamples/default/main.cpp b/examples/qml/referenceexamples/default/main.cpp index 6b47d0d4c4..82e6f5ba37 100644 --- a/examples/qml/referenceexamples/default/main.cpp +++ b/examples/qml/referenceexamples/default/main.cpp @@ -63,15 +63,15 @@ int main(int argc, char ** argv) auto *party = qobject_cast(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(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) - qWarning() << " " << party->guest(ii)->name(); + for (qsizetype ii = 0; ii < party->guestCount(); ++ii) + qInfo() << " " << party->guest(ii)->name(); return EXIT_SUCCESS; } diff --git a/examples/qml/referenceexamples/default/person.cpp b/examples/qml/referenceexamples/default/person.cpp index a1d887a4d0..e2847a2682 100644 --- a/examples/qml/referenceexamples/default/person.cpp +++ b/examples/qml/referenceexamples/default/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" -Person::Person(QObject *parent) -: QObject(parent), m_shoeSize(0) -{ -} +#include "person.h" QString Person::name() const { @@ -73,16 +69,3 @@ void Person::setShoeSize(int s) { m_shoeSize = s; } - - -Boy::Boy(QObject * parent) -: Person(parent) -{ -} - - -Girl::Girl(QObject * parent) -: Person(parent) -{ -} - diff --git a/examples/qml/referenceexamples/default/person.h b/examples/qml/referenceexamples/default/person.h index 361a89c599..0b2e280747 100644 --- a/examples/qml/referenceexamples/default/person.h +++ b/examples/qml/referenceexamples/default/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 @@ -60,7 +61,7 @@ class Person : public QObject Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize) QML_ANONYMOUS public: - Person(QObject *parent = nullptr); + using QObject::QObject; QString name() const; void setName(const QString &); @@ -69,7 +70,7 @@ public: void setShoeSize(int); private: QString m_name; - int m_shoeSize; + int m_shoeSize = 0; }; class Boy : public Person @@ -77,7 +78,7 @@ class Boy : public Person Q_OBJECT QML_ELEMENT public: - Boy(QObject * parent = nullptr); + using Person::Person; }; class Girl : public Person @@ -85,7 +86,7 @@ class Girl : public Person Q_OBJECT QML_ELEMENT public: - Girl(QObject * parent = nullptr); + using Person::Person; }; #endif // PERSON_H diff --git a/examples/qml/referenceexamples/grouped/birthdayparty.cpp b/examples/qml/referenceexamples/grouped/birthdayparty.cpp index 81db8ab1b8..269f45e02c 100644 --- a/examples/qml/referenceexamples/grouped/birthdayparty.cpp +++ b/examples/qml/referenceexamples/grouped/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,12 +47,8 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ -#include "birthdayparty.h" -BirthdayParty::BirthdayParty(QObject *parent) -: QObject(parent), m_host(nullptr) -{ -} +#include "birthdayparty.h" Person *BirthdayParty::host() const { @@ -69,13 +65,12 @@ QQmlListProperty 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/grouped/birthdayparty.h b/examples/qml/referenceexamples/grouped/birthdayparty.h index f6834dad89..a8e0964365 100644 --- a/examples/qml/referenceexamples/grouped/birthdayparty.h +++ b/examples/qml/referenceexamples/grouped/birthdayparty.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 BIRTHDAYPARTY_H #define BIRTHDAYPARTY_H @@ -62,14 +63,14 @@ class BirthdayParty : public QObject Q_CLASSINFO("DefaultProperty", "guests") QML_ELEMENT public: - BirthdayParty(QObject *parent = nullptr); + using QObject::QObject; Person *host() const; void setHost(Person *); QQmlListProperty guests(); - int guestCount() const; - Person *guest(int) const; + qsizetype guestCount() const; + Person *guest(qsizetype) const; private: Person *m_host; diff --git a/examples/qml/referenceexamples/grouped/main.cpp b/examples/qml/referenceexamples/grouped/main.cpp index 63dbe988a4..6d4ec517cc 100644 --- a/examples/qml/referenceexamples/grouped/main.cpp +++ b/examples/qml/referenceexamples/grouped/main.cpp @@ -63,23 +63,23 @@ int main(int argc, char ** argv) auto *party = qobject_cast(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(party->host())) - qWarning() << "He is inviting:"; + qInfo() << "He is inviting:"; else - qWarning() << "She is inviting:"; + qInfo() << "She is inviting:"; Person *bestShoe = nullptr; - for (int ii = 0; ii < party->guestCount(); ++ii) { + for (qsizetype ii = 0; ii < party->guestCount(); ++ii) { Person *guest = party->guest(ii); - qWarning() << " " << guest->name(); + qInfo() << " " << guest->name(); if (!bestShoe || bestShoe->shoe()->price() < guest->shoe()->price()) bestShoe = guest; } if (bestShoe) - qWarning() << bestShoe->name() << "is wearing the best shoes!"; + qInfo() << bestShoe->name() << "is wearing the best shoes!"; return EXIT_SUCCESS; } diff --git a/examples/qml/referenceexamples/grouped/person.cpp b/examples/qml/referenceexamples/grouped/person.cpp index 0603644108..4e52da76c8 100644 --- a/examples/qml/referenceexamples/grouped/person.cpp +++ b/examples/qml/referenceexamples/grouped/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/grouped/person.h b/examples/qml/referenceexamples/grouped/person.h index df507c7386..ed77f8c4d8 100644 --- a/examples/qml/referenceexamples/grouped/person.h +++ b/examples/qml/referenceexamples/grouped/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 @@ -92,7 +94,7 @@ class Person : public QObject // ![1] QML_ANONYMOUS public: - Person(QObject *parent = nullptr); + using QObject::QObject; QString name() const; void setName(const QString &); @@ -108,7 +110,7 @@ class Boy : public Person Q_OBJECT QML_ELEMENT public: - Boy(QObject * parent = nullptr); + using Person::Person; }; class Girl : public Person @@ -116,7 +118,7 @@ class Girl : public Person Q_OBJECT QML_ELEMENT public: - Girl(QObject * parent = nullptr); + using Person::Person; }; #endif // PERSON_H diff --git a/examples/qml/referenceexamples/methods/birthdayparty.cpp b/examples/qml/referenceexamples/methods/birthdayparty.cpp index 4b30d31aeb..1308a09b72 100644 --- a/examples/qml/referenceexamples/methods/birthdayparty.cpp +++ b/examples/qml/referenceexamples/methods/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,12 +47,8 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ -#include "birthdayparty.h" -BirthdayParty::BirthdayParty(QObject *parent) -: QObject(parent), m_host(nullptr) -{ -} +#include "birthdayparty.h" // ![0] Person *BirthdayParty::host() const @@ -70,12 +66,12 @@ QQmlListProperty 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/methods/birthdayparty.h b/examples/qml/referenceexamples/methods/birthdayparty.h index 5c2f3c9def..b43308ac8f 100644 --- a/examples/qml/referenceexamples/methods/birthdayparty.h +++ b/examples/qml/referenceexamples/methods/birthdayparty.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 BIRTHDAYPARTY_H #define BIRTHDAYPARTY_H @@ -61,21 +62,21 @@ class BirthdayParty : public QObject Q_PROPERTY(QQmlListProperty guests READ guests) QML_ELEMENT public: - BirthdayParty(QObject *parent = nullptr); + using QObject::QObject; Person *host() const; void setHost(Person *); QQmlListProperty guests(); - int guestCount() const; - Person *guest(int) const; + qsizetype guestCount() const; + Person *guest(qsizetype) const; // ![0] Q_INVOKABLE void invite(const QString &name); // ![0] private: - Person *m_host; + Person *m_host = nullptr; QList m_guests; }; diff --git a/examples/qml/referenceexamples/methods/main.cpp b/examples/qml/referenceexamples/methods/main.cpp index e30e1d9fb1..32eb4831cc 100644 --- a/examples/qml/referenceexamples/methods/main.cpp +++ b/examples/qml/referenceexamples/methods/main.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,6 +47,7 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ + #include #include #include @@ -63,10 +64,10 @@ int main(int argc, char ** argv) auto *party = qobject_cast(component.create()); if (party && party->host()) { - qWarning() << party->host()->name() << "is having a birthday!"; - qWarning() << "They are inviting:"; - for (int ii = 0; ii < party->guestCount(); ++ii) - qWarning() << " " << party->guest(ii)->name(); + qInfo() << party->host()->name() << "is having a birthday!" + << "\nThey are inviting:"; + for (qsizetype ii = 0; ii < party->guestCount(); ++ii) + qInfo() << " " << party->guest(ii)->name(); return EXIT_SUCCESS; } diff --git a/examples/qml/referenceexamples/methods/person.cpp b/examples/qml/referenceexamples/methods/person.cpp index 7712363ebf..e2847a2682 100644 --- a/examples/qml/referenceexamples/methods/person.cpp +++ b/examples/qml/referenceexamples/methods/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" -Person::Person(QObject *parent) -: QObject(parent), m_shoeSize(0) -{ -} +#include "person.h" QString Person::name() const { @@ -73,4 +69,3 @@ void Person::setShoeSize(int s) { m_shoeSize = s; } - diff --git a/examples/qml/referenceexamples/methods/person.h b/examples/qml/referenceexamples/methods/person.h index 2407fbb1b9..0849d1ee8c 100644 --- a/examples/qml/referenceexamples/methods/person.h +++ b/examples/qml/referenceexamples/methods/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 @@ -60,16 +61,17 @@ class Person : public QObject Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize) QML_ELEMENT public: - Person(QObject *parent = nullptr); + using QObject::QObject; QString name() const; void setName(const QString &); int shoeSize() const; void setShoeSize(int); + private: QString m_name; - int m_shoeSize; + int m_shoeSize = 0; }; #endif // PERSON_H diff --git a/examples/qml/referenceexamples/properties/birthdayparty.cpp b/examples/qml/referenceexamples/properties/birthdayparty.cpp index 2dc9bd803d..aca626db74 100644 --- a/examples/qml/referenceexamples/properties/birthdayparty.cpp +++ b/examples/qml/referenceexamples/properties/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,12 +47,8 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ -#include "birthdayparty.h" -BirthdayParty::BirthdayParty(QObject *parent) -: QObject(parent), m_host(nullptr) -{ -} +#include "birthdayparty.h" // ![0] Person *BirthdayParty::host() const @@ -76,11 +72,11 @@ QQmlListProperty BirthdayParty::guests() &BirthdayParty::removeLastGuest}; } -void BirthdayParty::appendGuest(Person* p) { +void BirthdayParty::appendGuest(Person *p) +{ m_guests.append(p); } - qsizetype BirthdayParty::guestCount() const { return m_guests.count(); @@ -107,12 +103,14 @@ void BirthdayParty::removeLastGuest() // ![0] -void BirthdayParty::appendGuest(QQmlListProperty* list, Person* p) { - reinterpret_cast< BirthdayParty* >(list->data)->appendGuest(p); +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(); +void BirthdayParty::clearGuests(QQmlListProperty* list) +{ + reinterpret_cast< BirthdayParty *>(list->data)->clearGuests(); } void BirthdayParty::replaceGuest(QQmlListProperty *list, qsizetype i, Person *p) @@ -125,10 +123,12 @@ void BirthdayParty::removeLastGuest(QQmlListProperty *list) reinterpret_cast< BirthdayParty* >(list->data)->removeLastGuest(); } -Person* BirthdayParty::guest(QQmlListProperty* list, qsizetype i) { +Person* BirthdayParty::guest(QQmlListProperty *list, qsizetype i) +{ return reinterpret_cast< BirthdayParty* >(list->data)->guest(i); } -qsizetype BirthdayParty::guestCount(QQmlListProperty* list) { +qsizetype BirthdayParty::guestCount(QQmlListProperty *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 7e1668add3..278c011d11 100644 --- a/examples/qml/referenceexamples/properties/birthdayparty.h +++ b/examples/qml/referenceexamples/properties/birthdayparty.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 BIRTHDAYPARTY_H #define BIRTHDAYPARTY_H @@ -69,28 +70,28 @@ class BirthdayParty : public QObject // ![3] QML_ELEMENT public: - BirthdayParty(QObject *parent = nullptr); + using QObject::QObject; Person *host() const; void setHost(Person *); QQmlListProperty guests(); - void appendGuest(Person*); + void appendGuest(Person *); qsizetype guestCount() const; Person *guest(qsizetype) const; void clearGuests(); - void replaceGuest(qsizetype, Person*); + void replaceGuest(qsizetype, Person *); void removeLastGuest(); private: - static void appendGuest(QQmlListProperty*, Person*); - static qsizetype guestCount(QQmlListProperty*); - static Person* guest(QQmlListProperty*, qsizetype); - static void clearGuests(QQmlListProperty*); - static void replaceGuest(QQmlListProperty*, qsizetype, Person*); - static void removeLastGuest(QQmlListProperty*); + static void appendGuest(QQmlListProperty *, Person *); + static qsizetype guestCount(QQmlListProperty *); + static Person* guest(QQmlListProperty *, qsizetype); + static void clearGuests(QQmlListProperty *); + static void replaceGuest(QQmlListProperty *, qsizetype, Person *); + static void removeLastGuest(QQmlListProperty *); - Person *m_host; + Person *m_host = nullptr; QList m_guests; }; // ![3] diff --git a/examples/qml/referenceexamples/properties/main.cpp b/examples/qml/referenceexamples/properties/main.cpp index e30e1d9fb1..cd71497002 100644 --- a/examples/qml/referenceexamples/properties/main.cpp +++ b/examples/qml/referenceexamples/properties/main.cpp @@ -63,10 +63,10 @@ int main(int argc, char ** argv) auto *party = qobject_cast(component.create()); if (party && party->host()) { - qWarning() << party->host()->name() << "is having a birthday!"; - qWarning() << "They are inviting:"; - for (int ii = 0; ii < party->guestCount(); ++ii) - qWarning() << " " << party->guest(ii)->name(); + qInfo() << party->host()->name() << "is having a birthday!\n" + "They are inviting:"; + for (qsizetype ii = 0; ii < party->guestCount(); ++ii) + qInfo() << " " << party->guest(ii)->name(); return EXIT_SUCCESS; } diff --git a/examples/qml/referenceexamples/properties/person.cpp b/examples/qml/referenceexamples/properties/person.cpp index 7712363ebf..e2847a2682 100644 --- a/examples/qml/referenceexamples/properties/person.cpp +++ b/examples/qml/referenceexamples/properties/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" -Person::Person(QObject *parent) -: QObject(parent), m_shoeSize(0) -{ -} +#include "person.h" QString Person::name() const { @@ -73,4 +69,3 @@ void Person::setShoeSize(int s) { m_shoeSize = s; } - diff --git a/examples/qml/referenceexamples/properties/person.h b/examples/qml/referenceexamples/properties/person.h index 2407fbb1b9..0849d1ee8c 100644 --- a/examples/qml/referenceexamples/properties/person.h +++ b/examples/qml/referenceexamples/properties/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 @@ -60,16 +61,17 @@ class Person : public QObject Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize) QML_ELEMENT public: - Person(QObject *parent = nullptr); + using QObject::QObject; QString name() const; void setName(const QString &); int shoeSize() const; void setShoeSize(int); + private: QString m_name; - int m_shoeSize; + int m_shoeSize = 0; }; #endif // PERSON_H 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 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 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 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(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(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 diff --git a/examples/qml/referenceexamples/valuesource/birthdayparty.cpp b/examples/qml/referenceexamples/valuesource/birthdayparty.cpp index 7ad11066cb..ced34c0cbb 100644 --- a/examples/qml/referenceexamples/valuesource/birthdayparty.cpp +++ b/examples/qml/referenceexamples/valuesource/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,12 +47,8 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ -#include "birthdayparty.h" -BirthdayPartyAttached::BirthdayPartyAttached(QObject *object) -: QObject(object) -{ -} +#include "birthdayparty.h" QDate BirthdayPartyAttached::rsvp() const { @@ -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 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); } @@ -108,11 +98,10 @@ QString BirthdayParty::announcement() const void BirthdayParty::setAnnouncement(const QString &speak) { - qWarning() << qPrintable(speak); + qInfo().noquote() << speak; } BirthdayPartyAttached *BirthdayParty::qmlAttachedProperties(QObject *object) { return new BirthdayPartyAttached(object); } - diff --git a/examples/qml/referenceexamples/valuesource/birthdayparty.h b/examples/qml/referenceexamples/valuesource/birthdayparty.h index abd6363571..4c661499ce 100644 --- a/examples/qml/referenceexamples/valuesource/birthdayparty.h +++ b/examples/qml/referenceexamples/valuesource/birthdayparty.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 BIRTHDAYPARTY_H #define BIRTHDAYPARTY_H @@ -62,7 +63,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); @@ -83,14 +84,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 guests(); - int guestCount() const; - Person *guest(int) const; + qsizetype guestCount() const; + Person *guest(qsizetype) const; QString announcement() const; void setAnnouncement(const QString &); @@ -98,11 +99,12 @@ public: static BirthdayPartyAttached *qmlAttachedProperties(QObject *); void startParty(); + signals: void partyStarted(QTime time); private: - Person *m_host; + Person *m_host = nullptr; QList m_guests; }; diff --git a/examples/qml/referenceexamples/valuesource/happybirthdaysong.cpp b/examples/qml/referenceexamples/valuesource/happybirthdaysong.cpp index da09d3d7ba..635a50ef12 100644 --- a/examples/qml/referenceexamples/valuesource/happybirthdaysong.cpp +++ b/examples/qml/referenceexamples/valuesource/happybirthdaysong.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,13 +47,13 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ + #include "happybirthdaysong.h" #include -HappyBirthdaySong::HappyBirthdaySong(QObject *parent) -: QObject(parent), m_line(-1) +HappyBirthdaySong::HappyBirthdaySong(QObject *parent) : + QObject(parent) { - setName(QString()); auto *timer = new QTimer(this); QObject::connect(timer, &QTimer::timeout, this, &HappyBirthdaySong::advance); timer->start(1000); diff --git a/examples/qml/referenceexamples/valuesource/happybirthdaysong.h b/examples/qml/referenceexamples/valuesource/happybirthdaysong.h index 2600208968..146e336487 100644 --- a/examples/qml/referenceexamples/valuesource/happybirthdaysong.h +++ b/examples/qml/referenceexamples/valuesource/happybirthdaysong.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 HAPPYBIRTHDAYSONG_H #define HAPPYBIRTHDAYSONG_H @@ -66,7 +67,7 @@ class HappyBirthdaySong : public QObject, public QQmlPropertyValueSource // ![1] QML_ELEMENT public: - HappyBirthdaySong(QObject *parent = nullptr); + explicit HappyBirthdaySong(QObject *parent = nullptr); void setTarget(const QQmlProperty &) override; // ![1] @@ -78,7 +79,7 @@ private slots: void advance(); private: - int m_line; + qsizetype m_line = -1; QStringList m_lyrics; QQmlProperty m_target; QString m_name; diff --git a/examples/qml/referenceexamples/valuesource/main.cpp b/examples/qml/referenceexamples/valuesource/main.cpp index 2e9995fcd8..a100497659 100644 --- a/examples/qml/referenceexamples/valuesource/main.cpp +++ b/examples/qml/referenceexamples/valuesource/main.cpp @@ -64,14 +64,14 @@ int main(int argc, char ** argv) auto *party = qobject_cast(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(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; @@ -81,9 +81,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/valuesource/person.cpp b/examples/qml/referenceexamples/valuesource/person.cpp index 0603644108..4e52da76c8 100644 --- a/examples/qml/referenceexamples/valuesource/person.cpp +++ b/examples/qml/referenceexamples/valuesource/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/valuesource/person.h b/examples/qml/referenceexamples/valuesource/person.h index 7283f39f61..ed77f8c4d8 100644 --- a/examples/qml/referenceexamples/valuesource/person.h +++ b/examples/qml/referenceexamples/valuesource/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,21 +77,24 @@ 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 { Q_OBJECT Q_PROPERTY(QString name READ name WRITE setName) +// ![1] Q_PROPERTY(ShoeDescription *shoe READ shoe) +// ![1] QML_ANONYMOUS public: - Person(QObject *parent = nullptr); + using QObject::QObject; QString name() const; void setName(const QString &); @@ -106,7 +110,7 @@ class Boy : public Person Q_OBJECT QML_ELEMENT public: - Boy(QObject * parent = nullptr); + using Person::Person; }; class Girl : public Person @@ -114,7 +118,7 @@ class Girl : public Person Q_OBJECT QML_ELEMENT public: - Girl(QObject * parent = nullptr); + using Person::Person; }; #endif // PERSON_H -- cgit v1.2.3