PDA

View Full Version : What is the difference between QRect and QRectF?



rezas1000
9th September 2014, 17:07
Hello,What is the difference between QRect and QRectF? The following code work well. It is interesting.

#include "lines.h"
#include <QPainter>


Lines::Lines(QWidget *parent)
: QWidget(parent)
{

}

void Lines::paintEvent(QPaintEvent *e)
{
Q_UNUSED(e);
QPainter qp(this);
drawLines(&qp);
}

void Lines::drawLines(QPainter *qp)
{

QRect rect(100.5,100.5,200.2,200.3);
qp->drawRect(rect);
}

stampede
9th September 2014, 21:45
What is the difference between QRect and QRectF ?

The QRectF class defines a rectangle in the plane using floating point precision...
...
The QRect class defines a rectangle in the plane using integer precision...



QRect rect(100.5,100.5,200.2,200.3);
Double values are implicitly converted to integers. You can find detailed info here: Type Conversions (http://www.cplusplus.com/doc/tutorial/typecasting/)

rezas1000
10th September 2014, 16:12
I understand,But if the double value converted to integer,why the QRect and the QRectF were built? please explain,thank you.

faldzip
10th September 2014, 20:43
Check the link mentioned by stampede, all you need is there.

stampede
10th September 2014, 20:44
if the double value converted to integer,why the QRect and the QRectF were built?
Exactly the same reason you have integers and doubles, sometimes you want one, sometimes the other :)

d_stranz
10th September 2014, 23:15
why the QRect and the QRectF were built? please explain,thank you.

With Qt 4, the coordinate system used in painting was changed from integer-based (pixels) to floating point (world coordinates). This permitted scaling and other transformations to be specified and performed more precisely, without rounding errors. In order to not break all existing code, a new set of floating point coordinate classes (QRectF, QSizeF, QPointF, etc.) was introduced side-by-side with the integer versions, and painting operations were extended to use the new floating point classes without changing the pre-existing integer methods.