简体   繁体   English

隐藏小部件时无法调整窗口大小

[英]I can't get window to resize when I hide a widget

I found some code on here that shows an example of how you can get the window to resize when the widget is hidden, and it works for me.我在这里找到了一些代码,它显示了如何在隐藏小部件时调整窗口大小的示例,它对我有用。 Here is the code:这是代码:

from PyQt4 import QtCore, QtGui
import sys

class MainWindow(QtGui.QWidget):
    def __init__(self):
        self.app = QtGui.QApplication(sys.argv)
        super(MainWindow, self).__init__()

        self.button = QtGui.QPushButton('Show/Hide')
        self.button.setCheckable(True)
        self.frame = QtGui.QFrame()
        self.frame.setFixedHeight(100)
        self.layout = layout = QtGui.QVBoxLayout()
        layout2 = QtGui.QVBoxLayout()
        self.setLayout(layout)
        self.frame.setLayout(layout2)

        layout.addWidget(self.button)
        layout.addWidget(self.frame)
        layout.addStretch(1)
        layout2.addWidget(QtGui.QLabel('Yoyoyo'))

        self.button.toggled.connect(self.clickAction)

    def startup(self):
        self.show()
        sys.exit(self.app.exec_())

    def clickAction(self):
        checked = self.button.isChecked()
        if checked:
            self.frame.show()
        else:
            self.frame.hide()
        QtCore.QTimer.singleShot(0, self.resizeMe)

    def resizeMe(self):
        self.resize(self.minimumSizeHint())
if __name__ == "__main__":
    myApp = MainWindow()
    myApp.startup()

I then tried to modify this to match my existing code by separating the mainWindow class and the widget class.然后我尝试通过分离 mainWindow 类和小部件类来修改它以匹配我现有的代码。 Here is the code that does that.这是执行此操作的代码。

from PySide import QtGui,QtCore
import sys



class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.w = testW(self)
        self.setCentralWidget(self.w)
        self.show()



class testW(QtGui.QWidget):
    def __init__(self,parent):
        super(testW,self).__init__()
        self.parent = parent
        self.button = QtGui.QPushButton('Show/Hide')
        self.button.setCheckable(True)
        self.button.setChecked(True);
        self.frame = QtGui.QFrame()
        self.frame.setFixedHeight(100)
        self.layout = layout = QtGui.QVBoxLayout()
        layout2 = QtGui.QVBoxLayout()
        self.setLayout(layout)
        self.frame.setLayout(layout2)

        layout.addWidget(self.button)
        layout.addWidget(self.frame)
        layout.addStretch(1)
        layout2.addWidget(QtGui.QLabel('Yoyoyo'))

        self.button.toggled.connect(self.clickAction)

    def clickAction(self):
        checked = self.button.isChecked()
        if checked:
            self.frame.show()
        else:
            self.frame.hide()
        QtCore.QTimer.singleShot(0, self.resizeMe)

    def resizeMe(self):
        self.resize(self.minimumSizeHint())

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myApp = MainWindow()
    sys.exit(app.exec_())
    #time.sleep(1)

Running the first code does what I want it to.运行第一个代码做我想要的。 After I hide the widget, the window resizes to the correct size.隐藏小部件后,窗口将调整为正确的大小。 The second implementation of the code does not shrink and expand the window when I hide and show the widget.当我隐藏和显示小部件时,代码的第二个实现不会缩小和扩展窗口。 Is this because the MainWindow is in a separate class?这是因为 MainWindow 在一个单独的类中吗?

  1. Use size policies for your widgets.为您的小部件使用大小策略 For your example you can change UI creation code as follows:对于您的示例,您可以按如下方式更改 UI 创建代码:
class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.w = testW(self)
        self.w.setSizePolicy(
            QtWidgets.QSizePolicy.MinimumExpanding,
            QtWidgets.QSizePolicy.MinimumExpanding
        )
        self.setCentralWidget(self.w)
        self.show()

Please note new setSizePolicy call which say Qt layout engine how to change the size of your widget according to its content.请注意新的 setSizePolicy 调用,它说明Qt 布局引擎如何根据其内容更改小部件的大小。

  1. Unfortunately QMainWindow does not respect sizeHint automatically, but it is calculated properly, so you can adjustSize manually:不幸的是QMainWindow的不尊重sizeHint自动的,但它是正确计算的,所以你可以adjustSize手动:
    def clickAction(self):
        checked = self.button.isChecked()
        if checked:
            self.frame.show()
        else:
            self.frame.hide()
        QtCore.QTimer.singleShot(0, self.parent.adjustSize)

You do not need to resize your widget itself, because it will be resized according to the policy.您不需要调整小部件本身的大小,因为它将根据策略调整大小。 Even sizeHint will be calculated automatically so you need only to call adjustSize of QMainWindow.甚至 sizeHint 也会自动计算,因此您只需要调用 QMainWindow 的 adjustSize。

PS: I used PySide2 instead of PySide so the imports are different a little bit: PS:我使用 PySide2 而不是 PySide,所以导入有点不同:

from PySide2 import QtWidgets, QtCore

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在PyQT4中,如何隐藏子窗口小部件的窗口标题? - In PyQT4, how can I hide the window title of a child widget? 当我用鼠标调整 kivy 中的 window 大小时,如何自动调整小部件(按钮/标签)的文本大小 - How to resize automatically the text from a widget(button/label) when i resize the window in kivy with the mouse PyQt4:隐藏窗口小部件并调整窗口大小 - PyQt4: Hide widget and resize window 调整窗口大小时是否调整Tkinter小部件和画布的大小? - Resize Tkinter widget and canvas when window resize? 当条目小部件没有聚焦时,我如何隐藏列表框小部件? - How would I hide a listbox widget when an entry widget isn't focused? tkinter:如何修复窗口,以便在调整内容大小时不调整窗口大小? - tkinter: How do I fix my window so that the window doesn't resize when my content resizes? 当我调整窗口大小时,QRubberBand移动 - QRubberBand move when I resize window 当我尝试调整窗口大小时,pygame崩溃 - pygame crashes when i try to resize window 如何在Tkinter中调整根窗口的大小? - How can I resize the root window in Tkinter? 在 Tkinter 中调整窗口大小并使宠物居中时,如何动态调整窗口多边形和椭圆的大小? - How can I dynamically resize my window polygons and ovals when resizing the window in Tkinter and centering the pet?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM