绘制QGraphicsItem的边框
如何绘制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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
请注意, QPainter::drawRect() 的文档提到了实际宽度:
鉴于此,我想您会想要比仅使用边界矩形稍微复杂一些的东西:
Note that the docs for QPainter::drawRect() mention the actual width:
Given that, I'd imagine you'd want something slightly more complicated than just using the bounding rectangle:
图形和绘图形状的坐标系很难搞清楚。您经常会发现自己编写测试图以使其正确,但它所绘制的正是您告诉它绘制的内容。您需要了解坐标系。在此页面上,请特别注意
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
.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.我发现了我的问题。感谢您的所有建议,但现在我明白了,我的问题在其他地方。
我可以绘制一个
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 myQGraphicsView
(no matter if I usefitInView()
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
QGraphicsItem
s, which are alsoQGraphicsSvgItem
s at once.