Improve rounding of QRect::toRect

Avoid the dimensions of the rounded QRect being off by more than one
pixel. This ensures the aligned containing rect also contains the
rounded rect.

Task-number: QTBUG-56420
Change-Id: Ib79110e51ab80de2dc83d01ea83fc5fbf3852e75
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
Allan Sandfeld Jensen 2017-06-21 10:05:07 +02:00
parent 67ee72f314
commit 88e56d0932
2 changed files with 33 additions and 1 deletions

View File

@ -870,7 +870,7 @@ Q_DECL_CONSTEXPR inline bool operator!=(const QRectF &r1, const QRectF &r2) Q_DE
Q_DECL_CONSTEXPR inline QRect QRectF::toRect() const Q_DECL_NOTHROW
{
return QRect(qRound(xp), qRound(yp), qRound(w), qRound(h));
return QRect(QPoint(qRound(xp), qRound(yp)), QPoint(qRound(xp + w) - 1, qRound(yp + h) - 1));
}
Q_DECL_CONSTEXPR inline QRectF operator+(const QRectF &lhs, const QMarginsF &rhs) Q_DECL_NOTHROW

View File

@ -171,6 +171,7 @@ private slots:
void containsPointF_data();
void containsPointF();
void smallRects() const;
void toRect();
};
// Used to work around some floating point precision problems.
@ -4331,5 +4332,36 @@ void tst_QRect::smallRects() const
QVERIFY(r1 != r2);
}
void tst_QRect::toRect()
{
for (qreal x = 1.0; x < 2.0; x += 0.25) {
for (qreal y = 1.0; y < 2.0; y += 0.25) {
for (qreal w = 1.0; w < 2.0; w += 0.25) {
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);
const QRectF arect = rectf.toAlignedRect();
QVERIFY(qAbs(arect.x() - rectf.x()) < 1.0);
QVERIFY(qAbs(arect.y() - rectf.y()) < 1.0);
QVERIFY(qAbs(arect.width() - rectf.width()) < 2.0);
QVERIFY(qAbs(arect.height() - rectf.height()) < 2.0);
QVERIFY(qAbs(arect.right() - rectf.right()) < 1.0);
QVERIFY(qAbs(arect.bottom() - rectf.bottom()) < 1.0);
QVERIFY(arect.contains(rectf));
QVERIFY(arect.contains(rect));
}
}
}
}
}
QTEST_MAIN(tst_QRect)
#include "tst_qrect.moc"