QGraphicsPolygonItem 绘制一个开放(非封闭)多边形
我正在使用 QGraphicsPolygonItem,我注意到它总是将终点与起点连接起来。
我知道多边形术语正是这个意思,我正在寻找的是“折线”或“多边形链”。我在 QGraphicsItem 子类中没有发现类似的内容。
如何在 QGraphics Framework 中绘制多边形链?是否有 QGraphicsPolygonItem 的属性或具有此功能的类?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我有一个类似的问题,我通过使用 QGraphicsPathItem 类解决了它。在下面的代码中,polygon是一个非闭合的QPolygonF对象(即一个起点与终点不同的QPolygonF):
当显示这个QGraphicsPathItem对象时,起点是(在理论)与其终点断开。
抱歉,这个示例代码是用 Java 编写的;但机制应该与 C++ 中的相同。
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):
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++.
您可以使用 QPainterPath 并使用 lineTo 方法输入您的折线点,然后只需使用 QGraphicsPathItem 使其成为图形项。
或者,您也可能会考虑将多个 QGraphicsLineItem 合并到一个 QGraphicsItemGroup 中,但这会更困难,因为您需要注意将线条对齐在一起。
这是您要找的吗?
编辑:
QPainterPath 显然正在关闭路径,那么您只剩下一组线条。
EDIT2:
很抱歉让您感到困惑,但 HostileFork 似乎是对的 - 您只需使用 QPainterPath 并调用
pathItem->setBrush(QBrush(Qt::transparent));
来保持路径未填充。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.