diff options
author | Olivier De Cannière <olivier.decanniere@qt.io> | 2023-05-04 13:10:21 +0200 |
---|---|---|
committer | Olivier De Cannière <olivier.decanniere@qt.io> | 2023-05-04 14:01:25 +0200 |
commit | 8fab8cf39c9ce7247b8e5b72d123c9dd806ea870 (patch) | |
tree | fe88cbfb224394076631ccb4ccd995be5dfda515 /examples/qml/tutorials | |
parent | b153740606956da52ace34a43214b8c70dff5f9e (diff) |
Examples: Add equality an operator to Extending Qml Advanced tutorials
This allows for more meaningful checks for identical assignment in
Person::setShoe().
Amends: 405bd4299819e39397cea0090a9442fd4b6ce911
Pick-to: 6.5
Change-Id: Id731f3f9163fb311ff9b04e2bbf4786a3022a11b
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Diffstat (limited to 'examples/qml/tutorials')
8 files changed, 84 insertions, 4 deletions
diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced4-Grouped-properties/person.cpp b/examples/qml/tutorials/extending-qml-advanced/advanced4-Grouped-properties/person.cpp index fe3d19b58d..53cec6b192 100644 --- a/examples/qml/tutorials/extending-qml-advanced/advanced4-Grouped-properties/person.cpp +++ b/examples/qml/tutorials/extending-qml-advanced/advanced4-Grouped-properties/person.cpp @@ -60,6 +60,12 @@ void ShoeDescription::setPrice(qreal price) } } +bool ShoeDescription::operatorEqualsImpl(const ShoeDescription &lhs, const ShoeDescription &rhs) +{ + return lhs.m_size == rhs.m_size && lhs.m_color == rhs.m_color && lhs.m_brand == rhs.m_brand + && lhs.m_price == rhs.m_price; +} + QString Person::name() const { return m_name; @@ -80,7 +86,10 @@ ShoeDescription *Person::shoe() const void Person::setShoe(ShoeDescription *shoe) { - if (m_shoe != shoe) { + if (!shoe) + return; + + if (*m_shoe != *shoe) { m_shoe = shoe; emit shoeChanged(); } diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced4-Grouped-properties/person.h b/examples/qml/tutorials/extending-qml-advanced/advanced4-Grouped-properties/person.h index ecb4545097..4f040e491b 100644 --- a/examples/qml/tutorials/extending-qml-advanced/advanced4-Grouped-properties/person.h +++ b/examples/qml/tutorials/extending-qml-advanced/advanced4-Grouped-properties/person.h @@ -31,10 +31,21 @@ public: qreal price() const; void setPrice(qreal); + friend bool operator==(const ShoeDescription &lhs, const ShoeDescription &rhs) + { + return operatorEqualsImpl(lhs, rhs); + } + friend bool operator!=(const ShoeDescription &lhs, const ShoeDescription &rhs) + { + return !operatorEqualsImpl(lhs, rhs); + } + signals: void shoeChanged(); private: + static bool operatorEqualsImpl(const ShoeDescription &, const ShoeDescription &); + int m_size = 0; QColor m_color; QString m_brand; diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced5-Attached-properties/person.cpp b/examples/qml/tutorials/extending-qml-advanced/advanced5-Attached-properties/person.cpp index fe3d19b58d..53cec6b192 100644 --- a/examples/qml/tutorials/extending-qml-advanced/advanced5-Attached-properties/person.cpp +++ b/examples/qml/tutorials/extending-qml-advanced/advanced5-Attached-properties/person.cpp @@ -60,6 +60,12 @@ void ShoeDescription::setPrice(qreal price) } } +bool ShoeDescription::operatorEqualsImpl(const ShoeDescription &lhs, const ShoeDescription &rhs) +{ + return lhs.m_size == rhs.m_size && lhs.m_color == rhs.m_color && lhs.m_brand == rhs.m_brand + && lhs.m_price == rhs.m_price; +} + QString Person::name() const { return m_name; @@ -80,7 +86,10 @@ ShoeDescription *Person::shoe() const void Person::setShoe(ShoeDescription *shoe) { - if (m_shoe != shoe) { + if (!shoe) + return; + + if (*m_shoe != *shoe) { m_shoe = shoe; emit shoeChanged(); } diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced5-Attached-properties/person.h b/examples/qml/tutorials/extending-qml-advanced/advanced5-Attached-properties/person.h index ecb4545097..4f040e491b 100644 --- a/examples/qml/tutorials/extending-qml-advanced/advanced5-Attached-properties/person.h +++ b/examples/qml/tutorials/extending-qml-advanced/advanced5-Attached-properties/person.h @@ -31,10 +31,21 @@ public: qreal price() const; void setPrice(qreal); + friend bool operator==(const ShoeDescription &lhs, const ShoeDescription &rhs) + { + return operatorEqualsImpl(lhs, rhs); + } + friend bool operator!=(const ShoeDescription &lhs, const ShoeDescription &rhs) + { + return !operatorEqualsImpl(lhs, rhs); + } + signals: void shoeChanged(); private: + static bool operatorEqualsImpl(const ShoeDescription &, const ShoeDescription &); + int m_size = 0; QColor m_color; QString m_brand; diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source/person.cpp b/examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source/person.cpp index fe3d19b58d..53cec6b192 100644 --- a/examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source/person.cpp +++ b/examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source/person.cpp @@ -60,6 +60,12 @@ void ShoeDescription::setPrice(qreal price) } } +bool ShoeDescription::operatorEqualsImpl(const ShoeDescription &lhs, const ShoeDescription &rhs) +{ + return lhs.m_size == rhs.m_size && lhs.m_color == rhs.m_color && lhs.m_brand == rhs.m_brand + && lhs.m_price == rhs.m_price; +} + QString Person::name() const { return m_name; @@ -80,7 +86,10 @@ ShoeDescription *Person::shoe() const void Person::setShoe(ShoeDescription *shoe) { - if (m_shoe != shoe) { + if (!shoe) + return; + + if (*m_shoe != *shoe) { m_shoe = shoe; emit shoeChanged(); } diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source/person.h b/examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source/person.h index ecb4545097..4f040e491b 100644 --- a/examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source/person.h +++ b/examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source/person.h @@ -31,10 +31,21 @@ public: qreal price() const; void setPrice(qreal); + friend bool operator==(const ShoeDescription &lhs, const ShoeDescription &rhs) + { + return operatorEqualsImpl(lhs, rhs); + } + friend bool operator!=(const ShoeDescription &lhs, const ShoeDescription &rhs) + { + return !operatorEqualsImpl(lhs, rhs); + } + signals: void shoeChanged(); private: + static bool operatorEqualsImpl(const ShoeDescription &, const ShoeDescription &); + int m_size = 0; QColor m_color; QString m_brand; diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/person.cpp b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/person.cpp index fe3d19b58d..53cec6b192 100644 --- a/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/person.cpp +++ b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/person.cpp @@ -60,6 +60,12 @@ void ShoeDescription::setPrice(qreal price) } } +bool ShoeDescription::operatorEqualsImpl(const ShoeDescription &lhs, const ShoeDescription &rhs) +{ + return lhs.m_size == rhs.m_size && lhs.m_color == rhs.m_color && lhs.m_brand == rhs.m_brand + && lhs.m_price == rhs.m_price; +} + QString Person::name() const { return m_name; @@ -80,7 +86,10 @@ ShoeDescription *Person::shoe() const void Person::setShoe(ShoeDescription *shoe) { - if (m_shoe != shoe) { + if (!shoe) + return; + + if (*m_shoe != *shoe) { m_shoe = shoe; emit shoeChanged(); } diff --git a/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/person.h b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/person.h index 0ed76223ba..03c2dab953 100644 --- a/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/person.h +++ b/examples/qml/tutorials/extending-qml-advanced/advanced7-Foreign-objects-integration/person.h @@ -31,10 +31,21 @@ public: qreal price() const; void setPrice(qreal); + friend bool operator==(const ShoeDescription &lhs, const ShoeDescription &rhs) + { + return operatorEqualsImpl(lhs, rhs); + } + friend bool operator!=(const ShoeDescription &lhs, const ShoeDescription &rhs) + { + return !operatorEqualsImpl(lhs, rhs); + } + signals: void shoeChanged(); private: + static bool operatorEqualsImpl(const ShoeDescription &, const ShoeDescription &); + int m_size = 0; QColor m_color; QString m_brand; |