title: Resize QMainWindow to minimal size after content of layout changes
body: <p>I'm using a subclass of <code>QMainWindow</code>, in which I declared a central widget. This widget contains, among other things, a <code>QGridLayout</code>, which holds a set of buttons. The amount of buttons can grow or shrink, depending on the user's input. The spacing is set to zero, so that all buttons are clumped together. By default, it looks like this:</p>
  
  <p><img src="https://i.stack.imgur.com/xYRwZ.png" alt="Layout by default"></p>
  
  <p>If the amount of buttons is increased, the grid and window will grow too just fine; if, however, the amount of buttons is reduced, it will look like this:</p>
  
  <p><img src="https://i.stack.imgur.com/zyMxk.png" alt="Layout done wrong"></p>
  
  <p>Now I would like to resize the window/layout/widget so that all buttons may use the minimal space. I've tried various things, but all to no avail. I had a look at <a href="https://stackoverflow.com/questions/8776332/how-to-programatically-resize-a-qmainwindow-to-its-minimum-size">this</a> and <a href="https://stackoverflow.com/questions/7031787/how-to-resize-qmainwindow-after-removing-all-dockwidgets">this</a> question, as well at various threads in the Qt board, but none of them worked for me.</p>
  
  <p>My layout is build as follows:</p>
  
  <pre><code>self.grid = QtGui.QGridLayout()
  self.grid.setSpacing(0)
  
  hBox = QtGui.QHBoxLayout()
  hBox.addWidget(...)
  
  vBox = QtGui.QVBoxLayout(self.widget)
  vBox.addLayout(hBox)
  vBox.addLayout(self.grid)
  
  self.setCentralWidget(self.widget)
  </code></pre>
  
  <p>I tried resizing it with ...</p>
  
  <pre><code>self.widget.layout().activate()
  self.resize(self.minimumSize())
  # self.resize(self.sizeHint())
  </code></pre>
  
  <p>... and various other methods. I also tried setting the size policy of my window and grid.</p>