Я пытаюсь переместить свой прямоугольник QGraphicsScene с помощью анимации, чтобы создать впечатление, что он движется плавно. Но я не знаю, как это работает. Может ли кто-нибудь мне помочь? Я хотел бы знать, возможно ли анимировать экземпляр Qtransform. если это так, как я могу это сделать?
Вопросы:
1 - Как анимировать функцию перевода, которая перемещает прямоугольник моей сцены. Я хочу анимировать его, потому что хочу, чтобы он выглядел гладким.
2 - Можно ли анимировать экземпляр Qtransform?
вот мой код:
from PyQt5.QtWidgets import QWidget, QApplication, QHBoxLayout
from PyQt5 import QtGui
from PyQt5 import QtWidgets
from PyQt5 import QtCore
class Example(QWidget):
def __init__(self, parent=None):
super(Example, self).__init__(parent) # this widget has no parent
hbox = QHBoxLayout(self)
self.setMinimumHeight(500)
self.setMinimumWidth(500)
self.button = QtWidgets.QPushButton("move scene", self) # this button is responsible to emit a signal to animate the
# scene translation
self.button.clicked.connect(self.do) # connect to the animation
self.scene = QtWidgets.QGraphicsScene() # instantiate the scene
self.view = QtWidgets.QGraphicsView(self) # instantiate the view
self.view.setScene(self.scene)
hbox.addWidget(self.view) # insert into the layout
hbox.addWidget(self.button) # insert into the layout
self.r = self.view.mapToScene(self.view.viewport().rect()).boundingRect()# take the viewport bounding rectangle
self.view.setSceneRect(self.r) # define the viewport bounding rectangle as the initial scene rectangle
self.scene.addEllipse(self.r, QtGui.QPen(QtGui.QBrush(QtCore.Qt.darkRed), 10, join=QtCore.Qt.RoundJoin))
# draw an ellipse in our scene
self.scene.setBackgroundBrush(QtGui.QBrush(QtCore.Qt.CrossPattern)) # set a grid patter to turn easier to
# see the displacements of objects
def translateSceneSmooth(self, ds: tuple) -> None:
"""
ds = (dx, dy)
:param ds: is the differential in x and y.
:return: None
"""
self.view.setSceneRect(self.view.sceneRect().translated(*ds)) # I want that the animation pass a interpolation
# of a tuple here: starting at (0, 0) and ending at (100, 100)
# I don't know if this approach is correct.
# Because maybe it will not move 100 px.if I have a list of numbers in the form passing through the function:
# [0, 10, 20, 50, 70, 100] maybe it'll move 0+10+20+50+70+100 = 250 px
def do(self, duration=100):
"""
I want the scene rectangle to move smoothly
"""
print('Starting animation')
self._animation = QtCore.QVariantAnimation()
self._animation.setStartValue(0)
self._animation.setEndValue(1000)
self._animation.setDuration(duration)
self._animation.start(QtCore.QAbstractAnimation.DeleteWhenStopped)
self._animation.valueChanged.connect(self.translateSceneSmooth)
print('Ending animation')
if __name__ == "__main__":
app = QApplication([])
ex = Example()
ex.show()
app.exec_()