diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2020-06-24 13:38:42 +0200 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2020-10-12 13:00:43 +0200 |
commit | a0deaf42e2bd8b5d773e75ce5fd0851335f4ba9b (patch) | |
tree | 754cd070b8b9c8ad5cba4d965ff97713d2ee6900 /src/corelib/tools/qrect.h | |
parent | 927813fc958a1b6876ae442b6e5e48de842724fd (diff) |
Improve QRectF::toRect()
Implement the better rounding mechanism that was previously blocked
by requiring C++14 to be constexpr.
Change-Id: I4e5b179ce0703f5c0b41c3f0ea00d28dfe53740c
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'src/corelib/tools/qrect.h')
-rw-r--r-- | src/corelib/tools/qrect.h | 9 |
1 files changed, 8 insertions, 1 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 |