diff options
author | Olivier De Cannière <olivier.decanniere@qt.io> | 2023-05-04 13:10:21 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2023-05-04 14:51:47 +0000 |
commit | 961a42f5fa976dae387b8c82ce50533072d79a9f (patch) | |
tree | a44c73ef0f7711b01b464d534def649eb4eb501f /examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source | |
parent | 8e675e07683532f0c340bb32a66a7f8bc99add71 (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
Change-Id: Id731f3f9163fb311ff9b04e2bbf4786a3022a11b
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
(cherry picked from commit 8fab8cf39c9ce7247b8e5b72d123c9dd806ea870)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source')
-rw-r--r-- | examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source/person.cpp | 11 | ||||
-rw-r--r-- | examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source/person.h | 11 |
2 files changed, 21 insertions, 1 deletions
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; |