在QPropertyAnimation

时间:2018-12-07 02:45:41

标签: python pyqt pyqt5 qpropertyanimation

我似乎无法确定是什么原因导致与QPropertyAnimation和setVisible()发生这种真正奇怪的交互

我尝试使用.show()和.hide()。我得到相同的结果。

尝试创建可折叠的组框,可以将其切换为折叠和取消折叠。折叠效果很好

enter image description here

但是,正如我所希望的那样,不合拢真的很奇怪,并且如此完成

enter image description here

如果我不使用淡入淡出功能,那么效果会很好,但这并不是我想要的效果。希望让子对象淡出,因为父对象正在崩溃,反之亦然。

非常感谢您指出此点。

代码在下面。

Animateui.py

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(590, 464)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
        MainWindow.setSizePolicy(sizePolicy)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.groupBox.sizePolicy().hasHeightForWidth())
        self.groupBox.setSizePolicy(sizePolicy)
        self.groupBox.setMaximumSize(QtCore.QSize(16777215, 16777215))
        self.groupBox.setStyleSheet("")
        self.groupBox.setObjectName("groupBox")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.groupBox)
        self.verticalLayout.setSizeConstraint(QtWidgets.QLayout.SetNoConstraint)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setSpacing(0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.pushButton = QtWidgets.QPushButton(self.groupBox)
        self.pushButton.setObjectName("pushButton")
        self.verticalLayout.addWidget(self.pushButton, 0, QtCore.Qt.AlignRight|QtCore.Qt.AlignTop)
        self.textEdit = QtWidgets.QTextEdit(self.groupBox)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.textEdit.sizePolicy().hasHeightForWidth())
        self.textEdit.setSizePolicy(sizePolicy)
        self.textEdit.setMinimumSize(QtCore.QSize(568, 395))
        self.textEdit.setObjectName("textEdit")
        self.verticalLayout.addWidget(self.textEdit)
        self.textEdit.raise_()
        self.pushButton.raise_()
        self.gridLayout.addWidget(self.groupBox, 0, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)

animatemain.py

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from animateui import Ui_MainWindow

class AnimateTest(QtWidgets.QMainWindow, Ui_MainWindow):

    def __init__(self, parent=None):
        super().__init__()
        self.setupUi(self)

        self.pushButton.released.connect(lambda: self.animate(self.groupBox, self.textEdit))

    def animate(self, obj1, obj2):

        print(obj2.height())

        if obj1.height() > 100:
            self.anim = QtCore.QPropertyAnimation(obj1, b"geometry")
            self.anim.setDuration(500)
            self.anim.setStartValue(obj1.geometry())
            self.anim.setEndValue(QtCore.QRect(obj1.x(), obj1.y(), obj1.width(), obj1.height()-300))
            self.anim.setEasingCurve(QtCore.QEasingCurve.OutQuad)
            self.anim.start()

            self.effect = QtWidgets.QGraphicsOpacityEffect()
            obj2.setGraphicsEffect(self.effect)

            self.animation = QtCore.QPropertyAnimation(self.effect, b"opacity")
            self.animation.setDuration(500)
            self.animation.setStartValue(1)
            self.animation.setEndValue(0)
            self.animation.start()
            QtCore.QTimer.singleShot(500, lambda: obj2.setVisible(False))

        if obj1.height() < 300:

            self.anim = QtCore.QPropertyAnimation(obj1, b"geometry")
            self.anim.setDuration(300)
            self.anim.setStartValue(QtCore.QRect(obj1.x(), obj1.y(), obj1.width(), obj1.height()))
            self.anim.setEndValue(QtCore.QRect(obj1.x(), obj1.y(), obj1.width(), 422))
            self.anim.start()

            self.effect = QtWidgets.QGraphicsOpacityEffect()
            obj2.setGraphicsEffect(self.effect)
            self.animation = QtCore.QPropertyAnimation(self.effect, b"opacity")
            self.animation.setDuration(300)
            self.animation.setStartValue(0)
            self.animation.setEndValue(1)
            self.animation.start()
            obj2.setVisible(True)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    ani = AnimateTest()
    ani.show()
    sys.exit(app.exec_())

0 个答案:

没有答案