title: Qt: HBoxLayout - stop MainWindow from resizing to contents
body: <p>It seems most people are asking how to make their <code>QMainWindow</code> resize to its contents - I have the opposite problem, my MainWindow <em>does</em> resize and I don't know why.</p>
  
  <p>When I set my <code>QLabel</code> to a longer text, my mainwindow suddenly gets bigger, and I can't find out why that happens.</p>
  
  <p>The following example code basically contains:</p>
  
  <ul>
  <li>A <code>QMainWindow</code>
  <ul>
  <li>A <code>QWidget</code> as central widget
  <ul>
  <li>A <code>QVBoxLayout</code> as a child of that
  <ul>
  <li>A <code>LabelBar</code> inside that.</li>
  </ul></li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p>The <code>LabelBar</code> is a <code>QWidget</code> which in turn contains:</p>
  
  <ul>
  <li>A <code>QHBoxLayout</code>
  <ul>
  <li>Two <code>QLabel</code>s in that.</li>
  </ul></li>
  </ul>
  
  <p>After a second, a <code>QTimer</code> sets the label to a longer text to demonstrate the issue.</p>
  
  <p>PyQt example code:</p>
  
  <pre class="lang-python prettyprint-override"><code>from PyQt5.QtWidgets import (QApplication, QHBoxLayout, QLabel, QWidget,
                               QMainWindow, QVBoxLayout, QSizePolicy)
  from PyQt5.QtCore import QTimer
  
  class MainWindow(QMainWindow):
      def __init__(self):
          super().__init__()
          cwidget = QWidget(self)
          self.setCentralWidget(cwidget)
          self.resize(100, 100)
  
          vbox = QVBoxLayout(cwidget)
          vbox.addWidget(QWidget())
          self.bar = LabelBar(self)
          vbox.addWidget(self.bar)
  
          timer = QTimer(self)
          timer.timeout.connect(lambda: self.bar.lbl2.setText("a" * 100))
          timer.start(1000)
  
  class LabelBar(QWidget):
      def __init__(self, parent=None):
          super().__init__(parent)
          hbox = QHBoxLayout(self)
          self.lbl1 = QLabel(text="eggs")
          hbox.addWidget(self.lbl1)
          self.lbl2 = QLabel(text="spam")
          hbox.addWidget(self.lbl2)
  
  if __name__ == '__main__':
      app = QApplication([])
      main = MainWindow()
      main.show()
      app.exec_()
  </code></pre>