让QGraphicsView做平滑的centerOn

时间:2010-11-05 08:53:45

标签: qt pyqt qgraphicsview

我在Qt不是新手,但有一些我不知道的事情...... 我正在用Python编程,但随时可以用任何语言发布你的答案。

所以,我有一些 QGraphicsItem (s),位于 QGraphicsScene 内,用正常的 QGraphicsView 查看场景。一切都很正常。

我的场景非常大,10,000 x 10,000像素,所有图形项目都散落在周围。

例如:

 # Creating Scene object.
 scene = QtGui.QGraphicsScene()
 scene.setSceneRect(0, 0, 10000, 10000)
 # Creating View object.
 view = QtGui.QGraphicsView()
 view.setScene(scene)

 # Adding some objects to scene.
 # scene.addItem(...)
 # ...

 # The list of items.
 items = scene.items()

 # This is how i center on item.
 view.centerOn(some_item)
 view.fitInView(some_item, Qt.KeepAspectRatio)

我的问题是,如何我可以使用与 centerOn 类似的内容将每个项目的视图集中在一起,但是顺利吗?

目前,centerOn对下一个项目进行了快速操作,我希望将它移动得很懒,可能使用 QPropertyAnimation 和缓动曲线?

我尝试使用大字幕中的 view.translate(1,1)将视图移动到下一个项目,但移动速度太快,就像centerOn一样。

我试图在翻译之间等待 time.sleep(0.01),但是Windows阻止直到cicle存在......所以它很糟糕。

非常感谢!

2 个答案:

答案 0 :(得分:2)

我曾经使用QTimeLine(使用EaseInOutCurve),将它连接到一个插槽,然后使用该值来翻译视图rect,如下所示:

const QRectF r = ...calculate rect translated by timeline value and trajectory...
view->setSceneRect( r );
view->fitInView( r, Qt::KeepAspectRatio );

对于轨迹,我使用了具有开始和结束位置的QLineF进行平滑滚动。 然后可以使用QLineF :: pointAt()很好地使用时间轴发出的值。

在我的情况下,我需要设置SceneRect和fitInView,使其表现得像我想要的那样。

答案 1 :(得分:1)

我通过在setSceneRect上放置一个高值来解决我的问题。然后我将场景集中在一个物体或位置上。

示例:

this-> ui-> graphicsView-> setSceneRect(0,0,100000000,1000000); 这 - > UI-> graphicsView-> centerOn(500,1030);

使用setSceneRect大小,GraphicsView无法正常工作。