I found your question because I searched the Internet for tips that I could use.
I took a look at the sources of Qt 4.7.4:
QGraphicsRectItemPrivate has a QRectF rect member QRectF rect , which can be obtained using rect () and changed using setRect ().- But since it is derived from
QGraphicsItem , it also has QRectF pos . - The
boundingRect() function returns a rect adjusted by the width of the pen, which means that the area to be updated is generally correct.
So, as Aldo said, you need to decide whether you use rect.topLeft or pos for positioning.
To help you and others decide, here are a few pro and opposing points:
Benefits of using rect:
- You can determine the position and size of the QGraphicsRectItem in a single function call.
- In the case of
QGraphicsLineItem , the pos () function always points to the top corner of the bounding edge rectangle, which does not have to be one of the points on the line, therefore, leaving pos in QPointF (0,0), the QLine points always refer to the scene.
The disadvantage of using rect:
- Since you cannot use setPos (), there is no function that emits
QGraphicsItem::ItemPositionChange . For me, this moment weighs more than advantages if you do not use QGraphicsLineItem (if I need QGraphicsLineItem, I would probably extract it from it and send QGraphicsItem :: ItemPositionChange myself).
How and when to use both:
If you use both pos and rect, let pos become the center of your GraphicsItem.
Imagine a marker, cursor or similar with a certain radius in your graphic scene. If you want to use QGraphicsRectItem for this, I suggest:
QPointF markerpos = GetMarkerPos(); double r = GetMarkerRadius(); QRectF markerrect(-r, -r, +r * 2., +r * 2.);
Thus, pos always points to the center, and the line is determined only by the radius.
source share