سؤال

A code below creates a single QWidget window with QPushButton connected to resizeDialog(). Pressing a button switches a dialog's size from .resize(200,100) to .resize(600,300) and back.

Question: By default the transition from one window's size to another happens instantly. How to override this behavior with an animation?

enter image description here

from PyQt4 import QtCore, QtGui
import sys

class myWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        super(myWindow, self).__init__(parent)
        myLayout = QtGui.QVBoxLayout(self)
        Button = QtGui.QPushButton('Resize')
        myLayout.addWidget(Button)
        Button.setMinimumWidth(200)
        Button.clicked.connect(self.resizeDialog)

    def resizeDialog(self):
        dialog.size().width()==200:
            dialog.resize(600,300)
        else:
            dialog.resize(200,100)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('myApp')
    dialog = myWindow()
    dialog.resize(200,100)
    dialog.show()
    sys.exit(app.exec_())
هل كانت مفيدة؟

المحلول

def resizeDialog(self):
    self.animation = QtCore.QPropertyAnimation(self, "size")
    # self.animation.setDuration(1000) #Default 250ms
    if self.size().width()==200:
        self.animation.setEndValue(QtCore.QSize(600,300))
    else:
        self.animation.setEndValue(QtCore.QSize(200,100))
    self.animation.start()

The source linked by this question, leads to this code. Translating from the c++ code to python is fairly straight forward in this case.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top