如何使用 QGraphicsView 的 QGraphicsItem 类绘制三角形

发布于 2024-09-28 05:40:45 字数 100 浏览 0 评论 0原文

我想使用QGraphicsItemQGraphicsView中绘制一个三角形对象。但我不知道如何根据triangler实现边界矩形。

I want to draw a triangular object in QGraphicsView by using QGraphicsItem. But I don't know how to implement bounding rect according to triangler.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

月亮邮递员 2024-10-05 05:40:45

您可以使用QGraphicsPolygonItem

您只需使用 QPolygonF 描述三角形多边形,然后使用 QGraphicsScene::addPolygon()

// Describe a closed triangle
QPolygonF Triangle;
Triangle.append(QPointF(-10.,0));
Triangle.append(QPointF(0.,-10));
Triangle.append(QPointF(10.,0));
Triangle.append(QPointF(-10.,0));

// Add the triangle polygon to the scene
QGraphicsPolygonItem* pTriangleItem = pScene->addPolygon(Triangle);

这样,一切都由 Qt 处理,您不必担心边界矩形。

You could use a QGraphicsPolygonItem.

You just have to describe a triangle polygon with QPolygonF and then add it to your scene with QGraphicsScene::addPolygon().

// Describe a closed triangle
QPolygonF Triangle;
Triangle.append(QPointF(-10.,0));
Triangle.append(QPointF(0.,-10));
Triangle.append(QPointF(10.,0));
Triangle.append(QPointF(-10.,0));

// Add the triangle polygon to the scene
QGraphicsPolygonItem* pTriangleItem = pScene->addPolygon(Triangle);

This way, everything is handled by Qt, you don't have to worry about bounding rect.

醉城メ夜风 2024-10-05 05:40:45

要绘制三角形,您需要 3 个点并在它们之间画一条线。子类化QGraphicsItem,并在子类的paint方法中绘制三角形,然后将项目设置为QGraphicsScene,然后将场景添加到QGraphicsView

To draw triangle you need 3 points and draw the line between them. Subclass the QGraphicsItem and in the paint method of subclass class draw triangle later set the item to QGraphicsScene then add the scene to QGraphicsView.

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