Examples: Add equality an operator to Extending Qml Advanced tutorials

This allows for more meaningful checks for identical assignment in
Person::setShoe().

Amends: 405bd42998

Change-Id: Id731f3f9163fb311ff9b04e2bbf4786a3022a11b
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
(cherry picked from commit 8fab8cf39c)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Olivier De Cannière 2023-05-04 13:10:21 +02:00 committed by Qt Cherry-pick Bot
parent 8e675e0768
commit 961a42f5fa
8 changed files with 84 additions and 4 deletions

View File

@ -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();
}

View File

@ -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;

View File

@ -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();
}

View File

@ -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;

View File

@ -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();
}

View File

@ -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;

View File

@ -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();
}

View File

@ -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;