开发者

Draw border of QGraphicsItem

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


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()));


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜