我有一个 QGraphicsScene “scene” 和 QGraphicsView “graphicsView”。
我有一个绘图方法。当需要重绘所有图形时,我调用此方法。一切正常。但是我意识到 scene->clear() 不会更改 sceneRect。
我也尝试过:
graphicsView->items().clear();
scene->clear();
graphicsView->viewport()->update();
接着,如果我通过以下方式获取sceneRect:
QRectF bound = scene->sceneRect();
qDebug() << bound.width();
qDebug() << bound.height();
我希望bound.width和bound.height的值为'0'。但它们并不是。我每次都看到之前的值。当我清除场景本身时,如何清除sceneRect?
使用graphicsView->fitInView()方法时,sceneRect保持不变会导致一些问题。我使用以下代码:
QRectF bounds = scene->sceneRect();
bounds.setWidth(bounds.width()*1.007); // to give some margins
bounds.setHeight(bounds.height()); // same as above
graphicsView->fitInView(bounds);
尽管我完全清除了场景并只添加了一个相当小的矩形,但由于sceneRect仍然太大,该矩形无法适合视图。
我希望我能解释清楚我的问题。