Michael Browne
Michael Browne

Reputation: 1

How do you keep the main window minimum size, but maintain the size of one widget *unless* the user is resizing the main window?

I apologize for the most confusing subject line ever, but I think I am fundamentally misunderstanding something about layouts and size hints.

I have a main window with one "principal" widget and another set of groupboxes that can be shown or hidden by the user. There are also some QLabels displaying status information that vary in size, depending on what is going on. The principal widget should be as large as possible, but it can be any size.

The behavior that I would like is that the principal widget should be constant size and the main window should grow or shrink whenever other widgets are shown/hidden or change size. However, when the user adjusts the size of the main window, the size of the principal widget should change to take all of the available space not taken by the other widgets.

If I set the principal widget's minimum size to its current size and resize the main window to the minimum size whenever the user hides/shows a groupbox, I have most of the behavior that I want, except the widget cannot be shrunk. If I let the principal widget's sizepolicy be Preferred and adjust the sizehint instead, resizing the main window to the minimum size always resizes the principal widget to its minimum size. My best effort so far attempts to set the minimum size before I make any changes to the other widgets, make the changes, resize the window to its minimum size, and then restore the "true" minimum size after things have "calmed down". I'm not 100% sure how to make sure things have calmed down though, as sending the posted events and then restoring the minimum size in a handler for a zero-time QTimer seems to be a bit glitchy and prone to races that I'm not really understanding. (Sometimes the window stays large after hiding something, and sometimes it seems to minimize the principal widget despite my efforts to protect it.)

I have the feeling that I'm just missing something fundamental and this should be much simpler than I am making it. Any assistance would be appreciated.

Upvotes: 0

Views: 333

Answers (1)

RodericDay
RodericDay

Reputation: 1292

from PySide import QtGui

class MainWindow(QtGui.QMainWindow):
  def __init__(self):
    super(MainWindow, self).__init__()

    self.setCentralWidget(QtGui.QLCDNumber())

    toolbar = QtGui.QToolBar("MyToolbar", self)
    button = QtGui.QPushButton("Hide Me")
    button.clicked.connect(toolbar.hide)

    toolbar.addWidget(button)
    self.addToolBar(toolbar)

if __name__=='__main__':
  import sys

  app = QtGui.QApplication(sys.argv)

  w = MainWindow()
  w.show()

  sys.exit(app.exec_())

does this help? It's a toolbar, but maybe the same principles propagate? edit: for such simple purposes, PySide and PyQt4 should be interchangeable.

Upvotes: 0

Related Questions