首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法调整QFrame大小

无法调整QFrame大小
EN

Stack Overflow用户
提问于 2020-06-06 20:29:36
回答 1查看 591关注 0票数 0

我想从应用程序启动时隐藏一个QFrame,然后使用函数将其显示为警告消息。但我找不到解决办法。帧继续显示,而不更改其起始大小。我对Qt Designer中的解决方案也很满意,但我不知道怎么做。self.frame_top_warning.setFixedHeight(0)似乎可以工作,但稍后在设置该帧的动画时会出现问题。

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2020-06-07 16:03:26

一种可能是将小部件的最大高度设置为0,并在开头使用self.frame_top_warning.setMaximumHeight(0)来隐藏QFrame。然后,您可以使用QtCore.QParallelAnimationGroup同时为minimumHeightmaximumHeight这两个属性设置动画。通过这种方式,您可以将小部件的高度限制为您想要的高度。

我举了一个小例子来说明我的意思。

代码语言:javascript
复制
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_())

希望这会有帮助=)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62231739

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档