Should QGraphicsItem::boundingRect() include child bounding rects?
Asked Answered
A

3

8

Googling suggests that it should.

But the dragdroprobot example implementation (on the parent Robot object) suggests not:

QRectF Robot::boundingRect() const
{
    return QRectF();
}

Which is correct, or is there something more subtle going on?

Armijo answered 16/9, 2011 at 17:27 Comment(1)
An update to the drag and drop exemple (the example with the robot). The link is here (September 01, 2016): doc.qt.io/qt-5/…Beehive
S
9

Child items are painted directly by the scene not by the parent, and according to the documentation about boundingRect():

QGraphicsView uses this to determine whether the item requires redrawing.

So, if there is no drawing to do in the parent, there is no need to return a non-null bounding rectangle, even if the parent has child items. And if there is some drawing in the parent, it only needs to contain its own bounding rectangle.

Stratocracy answered 16/9, 2011 at 18:15 Comment(2)
Okay, and if the parent has nothing to draw, by extension I can return nothing: but what if I want to allow all the children to be moved together, by setting the movable flag on the parent -- if it has no bounding rect, then mouse events never hit it?Armijo
Then, for example, in the parent you return a shape or a bounding rect containing the children, you add the flag QGraphicsItem::ItemHasNoContents and you stack the children behind the parent so the parent gets the mouse event before the children.Stratocracy
C
1

Under normal usage the children of your QGraphicsItem are contained within its bounding rect, but depending on your implementation I don't believe that this is required.

If you need the bounding rect of an item's children you can simply use

QGraphicsItem::childrenBoundingRect();
Catanzaro answered 16/9, 2011 at 21:4 Comment(0)
D
1

Possibly related:

  1. QGraphicItemGroup is different.

The documentation says:

The boundingRect() function of QGraphicsItemGroup returns the bounding rectangle of all items in the item group.

(However, the documentation does not say that boundingRect is reimplemented, although QGraphicsItemGroup inherits QGraphicsItem. Thats probably a flaw in the documentation.)

  1. QGraphicItem.shape() seems to be similar to boundingRect() in that the view calls it for each instance (for purposes of picking i.e. QGraphicsView.items(), similarly as boundingRect() is called for purposes of determining what needs to be redrawn).

As far as I can tell, QGraphicItemGroup.shape() is never called by QGraphicsView.items(). The documentation does not say that it is reimplemented.

Disjunctive answered 9/2, 2013 at 14:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.