我想从应用程序的开始隐藏QFrame,以后再将其显示为带有功能的警告消息。但是我找不到解决方案。框架会一直显示而不更改其开始尺寸。我也将对Qt Designer中的解决方案感到满意,但我不知道该怎么做。self.frame_top_warning.setFixedHeight(0)
似乎可以正常工作,但是稍后使该框架动起来时会出现问题。
class SampleApp(ui_main.Ui_MainWindow, QtWidgets.QMainWindow):
def __init__(self):
super(SampleApp, self).__init__()
self.setupUi(self)
# Here I want to set the start size to 0 to, later on, animate it in.
self.frame_top_warning.resize(self.frame_top_warning.width(), 0)
一种可能是将小部件的最大高度设置为0,并以self.frame_top_warning.setMaximumHeight(0)
开头以隐藏QFrame
。然后,您可以使用QtCore.QParallelAnimationGroup
同时为minimumHeight
和maximumHeight
的两个属性设置动画。通过这种方式,您可以将小部件的高度限制为您想要的高度。
我整理了一个小例子向您展示我的意思
import sys
from PyQt5 import QtWidgets, QtCore
class Widget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
layout = QtWidgets.QVBoxLayout()
show_warning_button = QtWidgets.QPushButton('Show Warning')
layout.addWidget(show_warning_button)
show_warning_button.clicked.connect(self.showWarningLabel)
layout.addWidget(QtWidgets.QPushButton('Button'))
layout.addWidget(QtWidgets.QLabel('This is some text'))
self.frame_top_warning = QtWidgets.QFrame()
self.frame_top_warning.setStyleSheet('QFrame {background: red;}')
self.frame_top_warning.setMaximumHeight(0)
layout.addWidget(self.frame_top_warning)
min_height_animation = QtCore.QPropertyAnimation(self.frame_top_warning, b"minimumHeight")
min_height_animation.setDuration(600)
min_height_animation.setStartValue(0)
min_height_animation.setEndValue(400)
max_height_animation = QtCore.QPropertyAnimation(self.frame_top_warning, b"maximumHeight")
max_height_animation.setDuration(600)
max_height_animation.setStartValue(0)
max_height_animation.setEndValue(400)
self.animation = QtCore.QParallelAnimationGroup()
self.animation.addAnimation(min_height_animation)
self.animation.addAnimation(max_height_animation)
self.setLayout(layout)
self.resize(800, 600)
self.show()
def showWarningLabel(self):
self.animation.start()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Widget()
sys.exit(app.exec_())
希望这有帮助=)