Reputation: 4497
How to draw border of QGraphicsItem
? Simple painter->drawRect( boundingRect() )
in overloaded paint method is not correct(bottom-right corner is outside the item).
Upvotes: 2
Views: 6561
Reputation: 22292
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
.
Upvotes: 4
Reputation: 46509
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()));
Upvotes: 4
Reputation: 1919
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.
Upvotes: 2
Reputation: 4497
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 QGraphicsItem
s, which are also QGraphicsSvgItem
s at once.
Upvotes: 0