- how QGraphicsItem knows when the bounding rect changes?
I think you know. Let's say you have a QGraphicsItemGroup aggregating several child items, and you want to show only one child at a time. The bounding rect of the group item needs to be the same as the bounding rect of the currently selected item:
QRectF MyItemGroup::boundingRect() const
{
QRectF rect;
if (currentItem_) {
rect = currentItem_->boundingRect();
}
return rect;
}
Suppose you have a method to change which one of the children has to be shown:
void MyItemGroup::setCurrentItem(MyItem * item)
{
if (list_of_items_.contains(item)) {
prepareGeometryChange();
currentItem_ = item;
}
}
If you comment out prepareGeometryChange, the view will not ask again for MyItemGroup's bounding rect, and the update of the item (triggered somewhere else) will involve the wrong rect.
- how it knows when to call update()?
According with the QGraphicsItem sources, inside the prepareGeometry
method update is called only in two cases:
- if someone is connected to the
changed
signal
- if the scene has no views
more details here
- Are you ever supposed to call update() yourself after calling
prepareGeometryChange() and then changing the bounding rectangle?
I think the answer is No. Aside from the call to update
, prepareGeometryChange
marks as dirty the item region on the scene, causing a repaint of the region itself. In my experience this sufficed in the 100% of the cases, so I didn't need to call update().
::boundingRect()
computation changes. Your example is right though. – Teshatesla