summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/corelib/tools/qrect.h9
-rw-r--r--tests/auto/corelib/tools/qrect/tst_qrect.cpp12
2 files changed, 14 insertions, 7 deletions
diff --git a/src/corelib/tools/qrect.h b/src/corelib/tools/qrect.h
index dbabc4eb0b..3e7b965829 100644
--- a/src/corelib/tools/qrect.h
+++ b/src/corelib/tools/qrect.h
@@ -874,7 +874,14 @@ constexpr inline bool operator!=(const QRectF &r1, const QRectF &r2) noexcept
constexpr inline QRect QRectF::toRect() const noexcept
{
- return QRect(QPoint(qRound(xp), qRound(yp)), QPoint(qRound(xp + w) - 1, qRound(yp + h) - 1));
+ // This rounding is designed to minimize the maximum possible difference
+ // in topLeft(), bottomRight(), and size() after rounding.
+ // All dimensions are at most off by 0.75, and topLeft by at most 0.5.
+ const int nxp = qRound(xp);
+ const int nyp = qRound(yp);
+ const int nw = qRound(w + (xp - nxp)/2);
+ const int nh = qRound(h + (yp - nyp)/2);
+ return QRect(nxp, nyp, nw, nh);
}
constexpr inline QRectF operator+(const QRectF &lhs, const QMarginsF &rhs) noexcept
diff --git a/tests/auto/corelib/tools/qrect/tst_qrect.cpp b/tests/auto/corelib/tools/qrect/tst_qrect.cpp
index 60c48a3f55..d65dba6e37 100644
--- a/tests/auto/corelib/tools/qrect/tst_qrect.cpp
+++ b/tests/auto/corelib/tools/qrect/tst_qrect.cpp
@@ -4373,12 +4373,12 @@ void tst_QRect::toRect()
for (qreal h = 1.0; h < 2.0; h += 0.25) {
const QRectF rectf(x, y, w, h);
const QRectF rect = rectf.toRect();
- QVERIFY(qAbs(rect.x() - rectf.x()) < 1.0);
- QVERIFY(qAbs(rect.y() - rectf.y()) < 1.0);
- QVERIFY(qAbs(rect.width() - rectf.width()) < 1.0);
- QVERIFY(qAbs(rect.height() - rectf.height()) < 1.0);
- QVERIFY(qAbs(rect.right() - rectf.right()) < 1.0);
- QVERIFY(qAbs(rect.bottom() - rectf.bottom()) < 1.0);
+ QVERIFY(qAbs(rect.x() - rectf.x()) <= 0.75);
+ QVERIFY(qAbs(rect.y() - rectf.y()) <= 0.75);
+ QVERIFY(qAbs(rect.width() - rectf.width()) <= 0.75);
+ QVERIFY(qAbs(rect.height() - rectf.height()) <= 0.75);
+ QVERIFY(qAbs(rect.right() - rectf.right()) <= 0.75);
+ QVERIFY(qAbs(rect.bottom() - rectf.bottom()) <= 0.75);
const QRectF arect = rectf.toAlignedRect();
QVERIFY(qAbs(arect.x() - rectf.x()) < 1.0);