如何在pyqt5的'QPropertyAnimation'中设置动画方向?

how to set animation direction in 'QPropertyAnimation' in pyqt5?

我成功地为我的小部件创建了一个动画,但我无法设置动画的方向。默认情况下,qwid1 小部件扩展到右侧,我想将动画的方向设置为左侧

import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *


class test(QWidget):
    def __init__(self):
        super().__init__()


        self.setGeometry(200, 200, 600, 700)

        self.b = QPushButton('expand', self)
        self.b.clicked.connect(self.expand)

        self.qwid1 = QWidget(self)
        self.qwid1.setGeometry(200, 60, 200, 400)
        self.qwid1.setStyleSheet(''' background-color: blue; ''')
    

    def expand(self):

        self.an = QPropertyAnimation(self.qwid1, b'geometry')
        self.an.setStartValue(self.qwid1.geometry())
        self.an.setEndValue(QRect(200, 60, 300, 400))
        self.an.setDuration(500)

        self.an.start()

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    my_test = test()
    my_test.show()
    app.exec()

如果分析一下初始和最终的QRect,问题就很清楚了:

  • 初始:200、200、600、700
  • 决赛:200、60、300、400

解决办法是构建最终的QRect,使“左边”比另一个小,其他保持不变。

def expand(self):

    an = QPropertyAnimation(self.qwid1, b"geometry", self)

    ri = QRect(self.qwid1.geometry())

    rf = ri.adjusted(-100, 0, 0, 0)

    an.setStartValue(ri)
    an.setEndValue(rf)
    an.setDuration(500)

    an.start(QAbstractAnimation.DeleteWhenStopped)