PDA

View Full Version : QPolygonF created from QRectF has five(?!) points



Asperamanca
3rd November 2010, 17:26
In order to use QTransform::quadToQuad, I convert two rectangles to QPolygonF.



void GenericScale::paintGrid(QPainter *painter,
QRectF logicalRect)
{
QPolygonF drawRect(logicalRect);

qDebug() << drawRect;
}


The resulting QPolygonF has - according to watch window and qDebug output - 5 points:



QPolygonF(QPointF(0, 1) QPointF(57, 1) QPointF(57, 2) QPointF(0, 2) QPointF(0, 1) )


Naturally, quadToQuad fails.

Why would the constructor of QPolygonF taking a QRectF as an argument create a QPolygonF with five instead of four points?

ecanela
3rd November 2010, 19:49
QPolygon consist in a QVector<QPoint>, convert a QRect to a QPolygon you need 5 point because a QRect is a closed shape, the first and last point are in the same position.

if you only need the 4 corners of the QRect, only remove the last qpoint.

sorry my poor english

Asperamanca
4th November 2010, 09:10
Hmmm...this doesn't quite make sense.
If you want a closed polygon, you still would only specify 4 points to fully define the polygon. You'd just have to specify it's a closed polygon (just as OpenGL does).

Why would I manipulate my data just to achieve something that should be a functionality of the class?

SixDegrees
4th November 2010, 09:45
QPolygons are not implicitly closed. If you want a closed QPolygon, you need an additional endpoint that coincides with the opposite endpoint.

Asperamanca
4th November 2010, 10:13
QPolygons are not implicitly closed. If you want a closed QPolygon, you need an additional endpoint that coincides with the opposite endpoint.

Ouch. This is ugly.

Thanks for the replies, though!

akiross
2nd April 2011, 17:30
Well I had the same question, the documentation a bit obscure:


QPolygonF::QPolygonF ( const QRectF & rectangle )
Constructs a closed polygon from the specified rectangle.
The polygon contains the four vertices of the rectangle in clockwise order starting and ending with the top-left vertex.
See also isClosed().

But looking ad isClosed:


bool QPolygonF::isClosed () const
Returns true if the polygon is closed; otherwise returns false.
A polygon is said to be closed if its start point and end point are equal.

So it says it have 4 vertex, but one is equal.
I wonder why QPolygon and QPolygonF are different on this aspect.

SixDegrees
2nd April 2011, 17:55
In nearly all computational geometry implementations, polygons are treated the same way Qt treats them, both in number of points and closed versus open count. Qt is simply following common practice here.