According to the Qt docs, it "Prepares the item for a geometry change. Call this function before changing the bounding rect of an item to keep QGraphicsScene's index up to date. prepareGeometryChange() will call update() if this is necessary."
What I don't understand is how QGraphicsItem knows when the bounding rect changes, and how it knows when to call update(). Are you ever supposed to call update() yourself after calling prepareGeometryChange() and then changing the bounding rectangle?
I'd greatly appreciate some insight into what appears to be a clairvoyant method.
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.
According with the QGraphicsItem sources, inside the prepareGeometry
method update is called only in two cases:
changed
signalmore details here
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().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With