summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/corelib/tools/qrect.cpp16
-rw-r--r--src/corelib/tools/qrect.h11
-rw-r--r--tests/auto/corelib/tools/qrect/tst_qrect.cpp25
3 files changed, 52 insertions, 0 deletions
diff --git a/src/corelib/tools/qrect.cpp b/src/corelib/tools/qrect.cpp
index c0e5127c6e..ce28a6d887 100644
--- a/src/corelib/tools/qrect.cpp
+++ b/src/corelib/tools/qrect.cpp
@@ -2437,6 +2437,22 @@ QRect QRectF::toAlignedRect() const noexcept
\sa marginsRemoved(), operator+=(), marginsAdded()
*/
+/*!
+ \fn bool QRectF::qFuzzyCompare(const QRectF &lhs, const QRectF &rhs)
+ \since 6.8
+
+ Returns \c true if the rectangle \a lhs is approximately equal to the
+ rectangle \a rhs; otherwise returns \c false.
+*/
+
+/*!
+ \fn bool QRectF::qFuzzyIsNull(const QRectF &rect)
+ \since 6.8
+
+ Returns \c true if both width and height of the rectangle \a rect are
+ approximately equal to zero; otherwise returns \c false.
+*/
+
/*****************************************************************************
QRectF stream functions
*****************************************************************************/
diff --git a/src/corelib/tools/qrect.h b/src/corelib/tools/qrect.h
index db70535a18..fb938b0056 100644
--- a/src/corelib/tools/qrect.h
+++ b/src/corelib/tools/qrect.h
@@ -585,6 +585,17 @@ private:
{ return r1.topLeft() == r2.topLeft() && r1.size() == r2.size(); }
Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(QRectF, QRect)
+ friend constexpr bool qFuzzyCompare(const QRectF &lhs, const QRectF &rhs) noexcept
+ {
+ return qFuzzyCompare(lhs.topLeft(), rhs.topLeft())
+ && qFuzzyCompare(lhs.bottomRight(), rhs.bottomRight());
+ }
+
+ friend constexpr bool qFuzzyIsNull(const QRectF &rect) noexcept
+ {
+ return qFuzzyIsNull(rect.w) && qFuzzyIsNull(rect.h);
+ }
+
public:
[[nodiscard]] constexpr inline QRect toRect() const noexcept;
[[nodiscard]] QRect toAlignedRect() const noexcept;
diff --git a/tests/auto/corelib/tools/qrect/tst_qrect.cpp b/tests/auto/corelib/tools/qrect/tst_qrect.cpp
index 43b46d8d8e..c7c8b3a560 100644
--- a/tests/auto/corelib/tools/qrect/tst_qrect.cpp
+++ b/tests/auto/corelib/tools/qrect/tst_qrect.cpp
@@ -38,8 +38,11 @@ private slots:
void comparisonCompiles();
void comparison_data();
void comparison();
+ void fuzzyComparison_data();
+ void fuzzyComparison();
void isNull_data();
void isNull();
+ void fuzzyIsNull();
void newIsEmpty_data();
void newIsEmpty();
void newIsValid_data();
@@ -309,6 +312,20 @@ void tst_QRect::comparison()
QT_TEST_EQUALITY_OPS(lhs, rhsF, mixedResult);
}
+void tst_QRect::fuzzyComparison_data()
+{
+ comparison_data();
+}
+
+void tst_QRect::fuzzyComparison()
+{
+ QFETCH(const QRectF, lhsF);
+ QFETCH(const QRectF, rhsF);
+ QFETCH(const bool, floatResult);
+
+ QCOMPARE_EQ(qFuzzyCompare(lhsF, rhsF), floatResult);
+}
+
void tst_QRect::isNull_data()
{
QTest::addColumn<QRect>("r");
@@ -338,6 +355,14 @@ void tst_QRect::isNull()
QVERIFY( rf.isNull() == isNull );
}
+void tst_QRect::fuzzyIsNull()
+{
+ QRectF rf(QPointF(-qreal_min, qreal_min), QPointF(qreal_min, -qreal_min));
+
+ QVERIFY(!rf.isNull()); // QRectF::isNull() does strict comparison
+ QVERIFY(qFuzzyIsNull(rf));
+}
+
void tst_QRect::newIsEmpty_data()
{
QTest::addColumn<QRect>("r");