RoomDroid
RoomDroid

Reputation: 27

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

I created an animation to my widget successfully but I can't set the dirction of the animation. the qwid1 widget extend to the right dirction by default and I want to set the dirction of the animation to the left like this enter image description here

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

Upvotes: 0

Views: 856

Answers (1)

eyllanesc
eyllanesc

Reputation: 243907

The problem is clear if you analyze the initial and final QRect:

  • initial: 200, 200, 600, 700
  • final: 200, 60, 300, 400

The solution is to build the final QRect so that the "left" is smaller than the other, and the others remain constant.

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)

Upvotes: 2

Related Questions