【发布时间】:2022-01-18 04:01:33
【问题描述】:
假设我有一个简单的 QGraphicsView 和 QGraphicsScene,我想为视图设置动画以平滑滚动/缩放到场景中的任意 QRecF。我知道您可以调用view.fitInView() 或view.centerOn() 在矩形上“加框”,但是它们都没有提供太多动画。我可以反复调用view.centerOn() 来很好地为滚动设置动画,但这不能处理缩放。我相信我需要修改视图的视口,但我不太确定该怎么做(调用view.setSceneRect() 似乎很有希望,但这只是设置视图可以管理的区域,而不是它当前“查看”的区域)。
基于一些 C++ 答案,我想出了如何在大多数情况下重写 fitInView 方法,但是我被困在我实际将视图转换为 look at 场景的不同部分的部分,而没有打电话给centerOn。这是我目前所拥有的:
view = QtWidgets.QGraphicsView()
scene = QtWidgets.QGraphicsScene()
targetRect = QtCore.QRectF(500, 500, 500, 500)
viewRect = view.viewport().rect()
sceneRect = view.transform().mapRect(targetRect)
xratio = viewRect.width() / sceneRect.width()
yratio = viewRect.height() / sceneRect.height()
# keep aspect
xratio = yratio = min(xratio, yratio)
# so... now what to do with the ratio? I need to scale the view rect somehow.
# animation
start = view.mapToScene(viewRect).boundingRect()
end = sceneRect # I guess?
animator = QtCore.QVariantAnimation()
animator.setStartValue(start)
animator.setEndValue(end)
animator.valueChanged.connect(view.????) # what am I setting here?
animator.start()
【问题讨论】:
标签: python animation pyqt qgraphicsview qgraphicsscene