[Solved] QGraphicsItem::boundingRect() and children
-
wrote on 13 Sept 2015, 17:01 last edited by Joel Bodenmann
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? -
wrote on 14 Sept 2015, 00:04 last edited by alex_malyu
You should return the bounding rect of all items.
Have you seen QGraphicsItemGroup?
This may provide an implementation you want. -
You should return the bounding rect of all items.
Have you seen QGraphicsItemGroup?
This may provide an implementation you want.wrote on 14 Sept 2015, 06:07 last edited byThat is exactly what I need. Thank you very much!
-
You should return the bounding rect of all items.
Have you seen QGraphicsItemGroup?
This may provide an implementation you want.wrote on 12 Dec 2018, 11:06 last edited byI'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());
-
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());
wrote on 12 Dec 2018, 14:29 last edited by@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.)