簡體   English   中英

如何為場景矩形設置動畫?

[英]How do I animate the scene Rectangle?

我正在嘗試使用 animation 移動我的 QGraphicsScene 矩形,以獲得它移動平穩的印象。 但我不知道該怎么做。 有人可以幫我嗎? 我想知道是否可以為 Qtransform 實例設置動畫。 如果是,我該怎么做?

問題:

1 - 如何為平移 function 設置動畫,這會移動我的場景矩形。 我想為它制作動畫,因為我希望它看起來很平滑。

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_()

如果要移動sceneRect,則通過計算開始和結束矩形來生成矩形的animation。 另一方面,默認情況下,按下的信號傳遞一個覆蓋默認值“duration”的 boolean,一個可能的解決方案是使用 pyqtSlot 裝飾器使連接簽名顯式:

from PyQt5 import QtCore, QtGui, QtWidgets


class Example(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Example, self).__init__(parent)

        self.setMinimumSize(500, 500)
        self.button = QtWidgets.QPushButton("move scene")
        # scene translation
        self.button.clicked.connect(self.do)
        self.scene = QtWidgets.QGraphicsScene()
        self.view = QtWidgets.QGraphicsView()
        self.view.setScene(self.scene)

        hbox = QtWidgets.QHBoxLayout(self)
        hbox.addWidget(self.view)
        hbox.addWidget(self.button)

        self.r = self.view.mapToScene(self.view.viewport().rect()).boundingRect()
        self.view.setSceneRect(self.r)
        self.scene.addEllipse(
            self.r,
            QtGui.QPen(QtGui.QBrush(QtCore.Qt.darkRed), 10, join=QtCore.Qt.RoundJoin),
        )
        self.scene.setBackgroundBrush(QtGui.QBrush(QtCore.Qt.CrossPattern))

    @QtCore.pyqtSlot()
    def do(self, duration=100):
        """
        I want the  scene rectangle to move smoothly
        """
        ds = QtCore.QPointF(100, 100)

        current_rect = self.view.sceneRect()
        next_rect = current_rect.translated(ds)
        self._animation = QtCore.QVariantAnimation()
        self._animation.setStartValue(current_rect)
        self._animation.setEndValue(next_rect)
        self._animation.setDuration(duration)
        self._animation.valueChanged.connect(self.view.setSceneRect)
        self._animation.finished.connect(lambda: print("Ending animation"))
        self._animation.start(QtCore.QAbstractAnimation.DeleteWhenStopped)
        print("Starting animation")


if __name__ == "__main__":
    app = QtWidgets.QApplication([])
    ex = Example()
    ex.show()
    app.exec_()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM