Even better, I think you can assign QGraphicsItem::childrenBoundingRect to the group's geometry directly. No additional calculations needed.
That is how they do it. Well, not exactly like that :
{
if (!item) {
qWarning("QGraphicsItemGroup::addToGroup: cannot add null item");
return;
}
if (item == this) {
qWarning("QGraphicsItemGroup::addToGroup: cannot add a group to itself");
return;
}
QTransform oldSceneMatrix = item->sceneTransform();
item->setPos(mapFromItem(item, 0, 0));
item->setParentItem(this);
item->setTransform(oldSceneMatrix
* sceneTransform().inverted()
* QTransform().translate(-item->x(), -item->y()));
item->d_func()->setIsMemberOfGroup(true);
prepareGeometryChange();
d->itemsBoundingRect |= (item->transform() * QTransform().translate(item->x(), item->y()))
.mapRect(item->boundingRect() | item->childrenBoundingRect());
update();
}
void QGraphicsItemGroup::addToGroup(QGraphicsItem *item)
{
Q_D(QGraphicsItemGroup);
if (!item) {
qWarning("QGraphicsItemGroup::addToGroup: cannot add null item");
return;
}
if (item == this) {
qWarning("QGraphicsItemGroup::addToGroup: cannot add a group to itself");
return;
}
QTransform oldSceneMatrix = item->sceneTransform();
item->setPos(mapFromItem(item, 0, 0));
item->setParentItem(this);
item->setTransform(oldSceneMatrix
* sceneTransform().inverted()
* QTransform().translate(-item->x(), -item->y()));
item->d_func()->setIsMemberOfGroup(true);
prepareGeometryChange();
d->itemsBoundingRect |= (item->transform() * QTransform().translate(item->x(), item->y()))
.mapRect(item->boundingRect() | item->childrenBoundingRect());
update();
}
To copy to clipboard, switch view to plain text mode
As you can see, the entire geometry update is done in one line.
Regards
Bookmarks