title: Qt - Don't stretch widgets in QVBoxLayout
body: <h2>Issue</h2>
  <p>When I drag to resize window in Qt, it is stretching the widgets inside my QVBoxLayout. I would like to avoid this.</p>
  <p>Here are a few pictures of the demo app (created for this stackoverflow post):</p>
  <h2>Screenshot: More Widgets than Space (works as expected -&gt; scoll down to view content)</h2>
  <p><a href="https://i.stack.imgur.com/XGxez.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XGxez.png" alt="more widgets than space" /></a></p>
  <h2>Screenshots: Less Widgets than Space (problem - Qt is stretching my AccountWidget items)</h2>
  <p><a href="https://i.stack.imgur.com/QSOOB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QSOOB.png" alt="less widgets than space" /></a></p>
  <p>How can I use Qt Layouts, or other Qt Widget classes to solve this problem? Here is the code for my Demo app (about 50 lines):</p>
  <pre><code>import sys
  from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, \
      QGroupBox, QGridLayout, QLabel, QScrollArea
  from typing import List
  
  class AccountWidget(QWidget):
      def __init__(self, data: dict, parent=None):
          super().__init__(parent)        
          self.group_box_layout = QGridLayout()
          for i, (key, value) in enumerate(data.items()):
              self.group_box_layout.addWidget(QLabel(key), i+1, 1)
              self.group_box_layout.addWidget(QLabel(value), i+1, 2)
          self.group_box = QGroupBox()
          self.group_box.setLayout(self.group_box_layout)
          self.main_layout = QVBoxLayout()
          self.main_layout.addWidget(self.group_box)
          self.setLayout(self.main_layout)
  
  class AccountListWidget(QWidget):
      def __init__(self, data: List[dict], parent=None):
          super().__init__(parent)
          self.main_layout = QVBoxLayout()
          for account_data in data:
              self.main_layout.addWidget(AccountWidget(account_data))
          self.setLayout(self.main_layout)
  
  class MainWidget(QWidget):
      def __init__(self, data: List[dict], parent=None):
          super().__init__(parent)
          self.account_list_widget = AccountListWidget(data)
          self.scroll_area = QScrollArea()
          self.scroll_area.setWidgetResizable(True)
          self.scroll_area.setWidget(self.account_list_widget)
          self.main_layout = QVBoxLayout()
          self.main_layout.addWidget(self.scroll_area)
          self.setLayout(self.main_layout)
  
  if __name__ == &quot;__main__&quot;:
      app = QApplication(sys.argv)
      data = [{&quot;created&quot;:&quot;2019&quot;,&quot;balance&quot;:&quot;100&quot;,&quot;deposits&quot;:&quot;50&quot;,&quot;type&quot;:&quot;Chq&quot;},
              {&quot;created&quot;:&quot;2020&quot;,&quot;balance&quot;:&quot;80&quot;,&quot;deposits&quot;:&quot;45&quot;,&quot;type&quot;:&quot;Chq&quot;},
              {&quot;created&quot;:&quot;2020&quot;,&quot;balance&quot;:&quot;70&quot;,&quot;deposits&quot;:&quot;55&quot;,&quot;type&quot;:&quot;Sav&quot;}]
      mainWidget = MainWidget(data)
      mainWindow = QMainWindow()
      mainWindow.setCentralWidget(mainWidget)
      mainWindow.setWindowTitle(&quot;Demo App&quot;)
      mainWindow.resize(300, 300)
      mainWindow.show()
      app.exec()
  </code></pre>