0

I would like to resize the MainWindow (QMainWindow) after I make some widgets unvisible and vice versa. And I want to block the window resize.

def hideAndShowWidget(self):
        self.widgetObject.setVisible(not self.widgetObject.isVisible() )
        # change main window size here
        # ...
        self.setFixedSize(self.width(), self.height())

My problem is, that i can not change the window size after i call setFixedSize() first time. I read here that I must use QWIDGETSIZE_MAX() to remove constraints, but I don't know how can I use it, I get the error:

NameError: name 'QWIDGETSIZE_MAX' is not defined
Igor
  • 358
  • 3
  • 6
  • 14

2 Answers2

1

Use the sizeHint(). It contains the size the widget would like to have. Set the fixed size exactly to the size hint.

Working example:

from PySide import QtGui

class Window(QtGui.QMainWindow):

    def __init__(self):
        super().__init__()
        self.setFixedSize(400, 300)

        widget = QtGui.QWidget()
        layout = QtGui.QVBoxLayout(widget)
        button = QtGui.QPushButton('Toggle visibility')
        button.clicked.connect(self.hideAndShowWidget)
        layout.addWidget(button)
        self.widgetObject = QtGui.QLabel('Test')
        layout.addWidget(self.widgetObject)
        self.setCentralWidget(widget)

    def hideAndShowWidget(self):
        self.widgetObject.setVisible(not self.widgetObject.isVisible() )
        # change main window size
        self.setFixedSize(self.sizeHint())


app = QtGui.QApplication([])
w = Window()
w.show()
app.exec_()
NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
  • Thanks! After the first click on the button in your example the window size is changed only once. Why? In my application it does not work: the window closes immediately, if i use `self.setFixedSize(self.sizeHint())`. Perhaps because my widget is greater then the visible area of the window? – Igor Jan 30 '15 at 09:29
1

I think you have the mechanism more or less right. You just have to make sure the height calculation is done correctly (i.e. before the visibility of the widget changes).

The following example works correctly for me (only tested on Linux, though):

from PySide import QtGui

class Window(QtGui.QWidget):
    def __init__(self):
        super(Window, self).__init__()
        self.widgetObject = QtGui.QTextEdit(self)
        self.button = QtGui.QPushButton('Hide Widget', self)
        self.button.clicked.connect(self.hideAndShowWidget)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.button)
        layout.addWidget(self.widgetObject)
        self.setFixedSize(300, 200)

    def hideAndShowWidget(self):
        height = self.height()
        if self.widgetObject.isVisible():
            height -= self.widgetObject.height()
            self.widgetObject.setVisible(False)
            self.button.setText('Show Widget')
        else:
            height += self.widgetObject.height()
            self.widgetObject.setVisible(True)
            self.button.setText('Hide Widget')
        self.setFixedSize(self.width(), height)

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())
ekhumoro
  • 115,249
  • 20
  • 229
  • 336