QGraphicsPolygonItem drawing a open(not closed) polygon
Asked Answered
C

2

8

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?

Coenocyte answered 11/10, 2011 at 14:27 Comment(0)
E
3

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.

Evonevonne answered 11/10, 2011 at 15:1 Comment(1)
QPainterPath doesn't close paths automatically, so using a sequence of lineTo and moveTo is probably the best approach. You may be confused because of a fill? If you don't want a fill, you have to call pathItem->setBrush(QBrush(Qt::transparent));Horripilation
V
6

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++.

Vale answered 12/10, 2011 at 2:26 Comment(0)
E
3

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.

Evonevonne answered 11/10, 2011 at 15:1 Comment(1)
QPainterPath doesn't close paths automatically, so using a sequence of lineTo and moveTo is probably the best approach. You may be confused because of a fill? If you don't want a fill, you have to call pathItem->setBrush(QBrush(Qt::transparent));Horripilation

© 2022 - 2024 — McMap. All rights reserved.