Should QGraphicsItem::boundingRect() include child bounding rects?

帅比萌擦擦* 提交于 2019-12-01 17:16:56

问题


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?


回答1:


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.




回答2:


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();



回答3:


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.



来源:https://stackoverflow.com/questions/7448321/should-qgraphicsitemboundingrect-include-child-bounding-rects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!