绘制QGraphicsItem的边框

发布于 2024-11-18 22:54:52 字数 118 浏览 3 评论 0原文

如何绘制QGraphicsItem的边框?重载的绘制方法中的简单 painter->drawRect(boundingRect() ) 不正确(右下角在项目之外)。

How to draw border of QGraphicsItem ? Simple painter->drawRect( boundingRect() ) in overloaded paint method is not correct(bottom-right corner is outside the item).

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

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

发布评论

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

评论(4

↙温凉少女 2024-11-25 22:54:52

请注意, QPainter::drawRect() 的文档提到了实际宽度:

填充矩形的大小为矩形.size()。描边矩形的大小为矩形.size() 加上画笔宽度。

鉴于此,我想您会想要比仅使用边界矩形稍微复杂一些的东西:

QRect r = boundingRect();
QPen p = painter->pen();
painter->drawRect(QRect(r.x(), r.y(), r.width()-p.width(), r.height()-p.width()));

Note that the docs for QPainter::drawRect() mention the actual width:

A filled rectangle has a size of rectangle.size(). A stroked rectangle has a size of rectangle.size() plus the pen width.

Given that, I'd imagine you'd want something slightly more complicated than just using the bounding rectangle:

QRect r = boundingRect();
QPen p = painter->pen();
painter->drawRect(QRect(r.x(), r.y(), r.width()-p.width(), r.height()-p.width()));
绝不放开 2024-11-25 22:54:52

图形和绘图形状的坐标系很难搞清楚。您经常会发现自己编写测试图以使其正确,但它所绘制的正是您告诉它绘制的内容。您需要了解坐标系。在此页面上,请特别注意 QRectF 的“一像素宽笔”图片。

The coordinate system for graphics and drawing shapes can be tricky to get straight. You will often find yourself programming test drawings to get it right but it is drawing precisely what you are telling it to draw. You need to understand the coordinate system. On this page, pay particular attention to the picture of "One pixel wide pen" for QRectF.

故人的歌 2024-11-25 22:54:52

QGraphicsEffect 可能是你的朋友。您可以将其子类化以在任意 QGraphicsItem 周围绘制边框。只需记住重新实现 boundingRectFor() 以包含额外的边框即可。

QGraphicsEffect may be your friend here. You can subclass it to draw a border around an arbitrary QGraphicsItem. Just remember to reimplement boundingRectFor() to include the extra border.

安稳善良 2024-11-25 22:54:52

我发现了我的问题。感谢您的所有建议,但现在我明白了,我的问题在其他地方。

我可以绘制一个QRectF(),它适合我的项目的boundingRect,但是......当我缩放我的QGraphicsView时(无论我是否使用fitInView( ) 方法,或我自己的实现)在显示我的边框时出现一些错误。

很少有一两行矩形比其他行更紧。我认为它可能与我的 QGraphicsItem 有关,它同时也是 QGraphicsSvgItem

I found out my problem. Thanks for all advises, but now I see, that my problem was elsewhere.

I can draw a QRectF(), which fits to boundingRect of my item, but... when I scale my QGraphicsView (no matter if I use fitInView() method, or my own implementation) there are some errors in displaying my border.

Rarely one or two lines of rect are tighter then others. I think that it can be be related to my QGraphicsItems, which are also QGraphicsSvgItems at once.

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