QGraphicsPolygonItem 绘制一个开放(非封闭)多边形

发布于 2024-12-09 03:43:45 字数 203 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悲凉≈ 2024-12-16 03:43:45

我有一个类似的问题,我通过使用 QGraphicsPathItem 类解决了它。在下面的代码中,polygon是一个非闭合的QPolygonF对象(即一个起点与终点不同的QPolygonF):

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

当显示这个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):

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

溺孤伤于心 2024-12-16 03:43:45

您可以使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文