pyqt5 如何控制隐藏组件显示时的大小

我需要内嵌一个网页,然后有一个按钮,用来显示或者隐藏这个网页
显示页面的时候最大化,隐藏页面的时候恢复到为显示页面时组件的大小

1、最开始只用了包含网页的容器self.ext的show和hidden,点击展示的时候根本无法页面,虽然能手动缩放,但是布局不可控
2、然后使用了
mainLayout.setSizeConstraint(QLayout.SetFixedSize)
解决了显示和隐藏的功能,但是不能缩放
3、尝试在显示和隐藏的时候对父组件使用resize,但是resize实际上重置组件的大小

import sys
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtCore import QUrl,Qt
from PyQt5.QtWidgets import QWidget, QLabel, QApplication,QMainWindow,QVBoxLayout,QHBoxLayout,QLineEdit,QPushButton,QLayout

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.initUi()

    def initUi(self):
        topLayout = QHBoxLayout()
        label = QLabel("号码")
        edit = QLineEdit()
        hidbutton = QPushButton("展示")

        hidbutton.clicked.connect(self.showHidd)

        topLayout.addWidget(label)
        topLayout.addWidget(edit)
        topLayout.addWidget(hidbutton)
        topLayout.addStretch()

        self.setWindowTitle("打开网页")
        self.browser = QWebEngineView()
        self.browser.load(QUrl("http://www.baidu.com"))

        extension = QVBoxLayout()
        extension.addWidget(self.browser)
        self.ext = QWidget()
        self.ext.setLayout(extension)

        mainLayout = QVBoxLayout()
        mainLayout.addLayout(topLayout)
        mainLayout.addWidget(self.ext)

        self.setLayout(mainLayout)
        #固定大小
        # mainLayout.setSizeConstraint(QLayout.SetFixedSize)
        self.ext.hide()

    def showHidd(self):
        if self.ext.isHidden():
            self.ext.show()
        else:
            self.ext.hide()

if __name__=='__main__':
    app = QApplication(sys.argv)
    win = MainWindow()
    win.show()
    sys.exit(app.exec_())

发现是个很简单的问题,对父组件self.showMaximized()和self.showNormal()就可以了

def showHidd(self):
    if self.ext.isHidden():
        self.ext.show()
        self.showMaximized()
    else:
        self.ext.hide()
        self.showNormal()
阅读 3.3k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题