Unsolved How to refresh QGraphicsItem bounding rectangle after scaling?
-
In my application I scale the item, but bounding rectangle does not change. How to make bounding rectangle change when scaling an item?
-
Hi
How did you implement it ? -
void ScaleTool::Update(QPointF currentPoint) { qreal currentDistance = std::sqrt(std::pow(rect().topLeft().x() - currentPoint.x(), 2) + std::pow(rect().topLeft().y() - currentPoint.y(), 2)); layer->setScale(currentDistance / startDistance); setRect(layer->boundingRect()); setPos(layer->pos()); }
ScaleTool is derrived from QGraphicsRectItem. I expect line
setRect(layer->boundingRect());
to set rectangle to fit layer's bounding rectangle, but it does not change (i checked it in qDebug). -
Hi
Normally you override the virtual function and return the rectQRectF CircleItem::boundingRect() const { qreal penWidth = 1; return QRectF(-radius - penWidth / 2, -radius - penWidth / 2, diameter + penWidth, diameter + penWidth); }
so does setRect(layer->boundingRect());
set a rect you then return in that boundingRect? -
Okay, I forgot to reimplement bounding rectangle. But, how can I "refresh" it once it changes?
-
@clostridium_difficile
well could you not just return QRectF QGraphicsRectItem::rect() const
since you set it in Update ?QRectF ScaleTool::boundingRect() const { return rect(); }
However, i would check what the base class (QGraphicsRectItem) actually returns
as it might already return rect and what is missing is a call to
https://doc.qt.io/qt-5/qgraphicsitem.html#prepareGeometryChange -
solution to my problem was returning mapped to scene bounding rectangle:
QRectF ImageLayer::boundingRect() const { return mapRectToScene(rect()); }
-
@clostridium_difficile
ah yes good catch. boundingRect is ofc in expected to be in scene coordinates.