Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QGraphicsPolygonItem drawing a open(not closed) polygon

I am using a QGraphicsPolygonItem and I have noticed that it always connects the end-point with the start-point.

I know that the polygon terms means exactly that, and what I am looking for is "polyline" or "polygonal chain". I didnt found nothing like that in QGraphicsItem subclasses.

How do I draw a polygonal chain in QGraphics Framework? Is there a property of QGraphicsPolygonItem or a class that does that?

like image 485
André Puel Avatar asked Oct 11 '11 14:10

André Puel


2 Answers

I had a similar problem, and I solved it by using the QGraphicsPathItem class. In the following code, polygon is a non-closed QPolygonF object (i.e. a QPolygonF which start-point is different from its end-point):

QPainterPath path = new QPainterPath();
path.addPolygon(polygon);
QGraphicsPathItem contour = new QGraphicsPathItem(path);
contour.setPen(new QPen(QColor.black));

When displaying this QGraphicsPathItem object, the start-point is (in theory) disconnected from its end-point.

I'm sorry this example code is in Java; but the mechanisms should be the same as in C++.

like image 143
Bertrand Moreau Avatar answered Oct 26 '22 13:10

Bertrand Moreau


You can use QPainterPath and use lineTo method to input yors polyline points, then just use QGraphicsPathItem to make it graphics item.

Alternatively you also might think about combining several QGraphicsLineItem into one QGraphicsItemGroup, but that's more difficult as you need to pay attention to aligning lines together.

Is this what you are looking for?

EDIT:

QPainterPath is apparently closing paths, then you are left with group of lines only.

EDIT2:

Sorry for confusing you, but HostileFork seem to be right - you just use QPainterPath and call pathItem->setBrush(QBrush(Qt::transparent)); to keep your path unfilled.

like image 33
j_kubik Avatar answered Oct 26 '22 14:10

j_kubik