title: How to avoid over-packing non-srolling Qt layouts?
body: <p>A Qt packing layout, such as <code>QVBoxLayout</code>, can pack widgets inside it, such as buttons.  In this case, they will be packed vertically as shown in image below:</p>
  <p><a href="https://i.stack.imgur.com/niWap.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/niWap.png" alt="enter image description here" /></a></p>
  <p>When we pack <em>too many</em> widgets inside such a layout, and since scrolling is not added by default, the buttons will eventually get squeezed onto each other up to a point that they will overlap, as shown below:</p>
  <p><a href="https://i.stack.imgur.com/DvreD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DvreD.png" alt="enter image description here" /></a></p>
  <p><em><strong>My questions are:</strong></em></p>
  <ol>
  <li>How to tell Qt to not show/pack widgets beyond the available viewing space in the non-scrolling layout?</li>
  <li>How to handle the case when the window is resized?  I.e. Qt should add/remove widgets accordingly.  E.g. if there is extra space available, then perhaps Qt should add some extra widgets that it couldn't add previously.</li>
  </ol>
  <p>To be specific:  &quot;too many packed widgets&quot; is when the widgets start invading spaces of other widgets, including their inter-widget spacings or margins.</p>
  <hr />
  <h2>Appendix</h2>
  <p>Images above are generated by this code below as run in a tile in <a href="https://i3wm.org" rel="nofollow noreferrer">i3</a>, which is a modified version of <a href="https://stackoverflow.com/a/72843580/5810023">this</a>.</p>
  <pre class="lang-py prettyprint-override"><code>from PyQt5 import QtCore, QtWidgets
  
  app = QtWidgets.QApplication([])
  widget = QtWidgets.QWidget()
  layout = QtWidgets.QVBoxLayout(widget)
  for i in range(40):
      layout.addWidget(QtWidgets.QPushButton(str(i + 1)))
  widget.show()
  app.exec_()
  </code></pre>
  <p>When too many widgets are packed:</p>
  <ul>
  <li>If the window is tiled, you see them overcrowded as in in the image.</li>
  <li>If the window is floating, the window will keep growing until it is no longer fully visible in the monitor.</li>
  </ul>
  <p>None of these outcomes are acceptable in my case.  My goal is to have Qt only pack as much as will be visible, and add/remove/hide/show dynamically as the window gets resized.</p>