[Solved] QGraphicsItem::boundingRect() and children
-
Is
QGraphicsItem::boundingRect()
supposed to return the bounding rect of just this item or does this need to include children items too?Let's assume that I have a
QGraphicsRectItem
with different children where the children are partially inside and partially outside of the rect items rectangle. Do I have to implement theboundingRect()
method so it takes the children into account as well? -
You should return the bounding rect of all items.
Have you seen QGraphicsItemGroup?
This may provide an implementation you want. -
That is exactly what I need. Thank you very much!
-
I'm sorry to bring up this old topic. However, after three more years of experience working with Qt & the Qt graphics framework I'd like to say that the original answer is incorrect.
QGraphicsItem::boundingRect()
must not return a rectangle that also contains the children. It must only return that item's (*this
) bounding rect without taking any children into account.
If there's a need to retrieve the bounding rect of all children, there'sQGraphicsItem::childrenBoundingRect()
. That rectangle will be a union of the bounding rects of ALL children but it will not contain the calling item's bounding rect.The following code allows to retrieve the bounding rect of an item and all it's children:
QRectF rectF = boundingRect(); rectF = rectF.united(childrenBoundingRect());
-
@Joel-Bodenmann
To elaborate on your answer: The boundingRect() must include every pixel that you intend to paint to in your own item's paint method. That includes e.g. pixels that are outside your item proper, but painted to due to a wide pen.
(Calculating the correct boundingRect for items with wide cosmetic pens can be a real pain.)