我想从应用程序启动时隐藏一个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)发布于 2020-06-07 16:03:26
一种可能是将小部件的最大高度设置为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_())希望这会有帮助=)
https://stackoverflow.com/questions/62231739
复制相似问题