Animating viewable rect of QGraphicsView
-
How can I animate the viewable scene rect of a QGraphicsView using screen coordinates? This means animating both center pos and scale. This is similar to Google Earth where the map scrolls and zooms smoothly from one point to another. I have searched for an answer for this several times in the last couple of years with little success.
The use case is that I use QGraphicsView::fitInView to show the bounding rect of all visible items. Then some items are hidden and I want to animate zooming/scrolling to fit the new bounding rect of all the items.
Simply setting up one animation to periodically call QGraphicsView::centerOn() and another to call QGraphicsView::scale() doesn't work because calling one displaces the values of the other as the interpolation progresses.
Thoughts? This seems like a pretty essential use case for QGraphicsView.
-
Posting always seems to inspire...
Turns out QGraphicsView::fitInView() actually sets the viewable rect, as opposed to simply ensuring that it "fits in the view" as implied. Therefore, you can simply setup an animation to periodically set the value. It appears very smooth here. Example:
anim = QVariantAnimation() anim.setStartValue(startRectF) anim.setEndvalue(endRectF) anim.valueChanged.connect(lambda x: view.fitInView(x, Qt.KeepAspectRatio) anim.start()
Simple. I didn't see this out there. It should really spice up some QGraphicsView apps out there.