调用 setSizes() 方法时如何为拆分器设置动画?

How to animate the splitter when setSizes() method is called?

我的 Qt5 GUI 中有 QSplitter 小部件,并且有两个子小部件添加到该拆分器。并且有一个 QPushButton 小部件,当按下该按钮时,第二个子部件消失,当再次按下该按钮时,该子部件出现。但我想要 扩展和折叠 那个子部件。但我不知道如何在拆分器小部件上调用 'setSizes()' 时为事件设置动画。

谁能给我提供 pyqt5 中的代码?

这是我的代码

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(QtWidgets.QMainWindow):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(400, 300)
        self.centralWidget = QtWidgets.QWidget(MainWindow)
        self.centralWidget.setObjectName("centralWidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.centralWidget)
        self.verticalLayout.setContentsMargins(11, 11, 11, 11)
        self.verticalLayout.setSpacing(6)
        self.verticalLayout.setObjectName("verticalLayout")

        self.pushButton = QtWidgets.QPushButton(self.centralWidget)
        self.pushButton.setObjectName("pushButton")
        self.pushButton.clicked.connect(self.splitter_resize)
        self.pushButton.setCheckable(True)

        self.verticalLayout.addWidget(self.pushButton)

        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setSpacing(6)
        self.horizontalLayout.setObjectName("horizontalLayout")

        self.pushButton_2 = QtWidgets.QPushButton(self.centralWidget)
        self.pushButton_2.setObjectName("pushButton_2")

        self.pushButton_3 = QtWidgets.QPushButton(self.centralWidget)
        self.pushButton_3.setObjectName("pushButton_3")

        self.splitter = QtWidgets.QSplitter()
        self.splitter.setOrientation(QtCore.Qt.Horizontal)
        self.splitter.addWidget(self.pushButton_2)
        self.splitter.addWidget(self.pushButton_3)

        self.horizontalLayout.addWidget(self.splitter)

        self.verticalLayout.addLayout(self.horizontalLayout)
        MainWindow.setCentralWidget(self.centralWidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def splitter_resize(self):
        if self.pushButton.isChecked():
            self.splitter.setSizes([16777215, 0])
            self.pushButton.setChecked = False
        else:
            self.splitter.setSizes([16777215, 16777215])
            self.pushButton.setChecked = True

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "PushButton"))
        self.pushButton_2.setText(_translate("MainWindow", "PushButton"))
        self.pushButton_3.setText(_translate("MainWindow", "PushButton"))

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

您可以使用 QVariantAnimation:

from PyQt5 import QtCore, QtGui, QtWidgets


MIN_SIZE, MAX_SIZE = 0, 16777215


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        button = QtWidgets.QPushButton(
            "Press me", checkable=True, toggled=self.onToggled
        )
        left_button = QtWidgets.QPushButton("Left")
        right_button = QtWidgets.QPushButton("Right")

        sp = right_button.sizePolicy()
        sp.setHorizontalPolicy(QtWidgets.QSizePolicy.Ignored)
        right_button.setSizePolicy(sp)

        self.m_splitter = QtWidgets.QSplitter(orientation=QtCore.Qt.Horizontal)
        self.m_splitter.addWidget(left_button)
        self.m_splitter.addWidget(right_button)

        widget = QtWidgets.QWidget()
        self.setCentralWidget(widget)

        lay = QtWidgets.QVBoxLayout(widget)
        lay.addWidget(button)
        lay.addWidget(self.m_splitter)

        self.m_animation = QtCore.QVariantAnimation(
            self,
            startValue=MAX_SIZE,
            endValue=MIN_SIZE,
            valueChanged=self.onValueChanged,
            duration=1000,
            easingCurve=QtCore.QEasingCurve.InOutCubic,
        )

    @QtCore.pyqtSlot(bool)
    def onToggled(self, checked):
        self.m_animation.setDirection(
            QtCore.QAbstractAnimation.Forward
            if checked
            else QtCore.QAbstractAnimation.Backward
        )
        self.m_animation.start()

    @QtCore.pyqtSlot(QtCore.QVariant)
    def onValueChanged(self, value):
        s = [MAX_SIZE, value]
        self.m_splitter.setSizes(s)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.resize(640, 480)
    w.show()
    sys.exit(app.exec_())