title: How to make QGridLayout resize cells the exact same size?
body: <p><a href="https://i.imgur.com/DbKd5ME.png" rel="nofollow noreferrer"><img src="https://i.imgur.com/DbKd5ME.png" alt="image representation of code" /></a></p>
  <p>Whenever I resize the window (a QDialog), Reference Viewer and Selected Viewer (subclasses of QScrollArea) should have the exact same size at all time, even after a resize event.
  However, <strong>once out of twice</strong>, I get a size 1 pixel smaller for the Selected Viewer (QScrollArea widget on the right). By once out of twice, I mean every odd pixel count.</p>
  <p>It seems that the QGridLayout is forcing the right-most panel to that smaller size, probably due to rounding down the value of the space still available.</p>
  <p>I use a QGridLayout because I need the toolbar to stay aligned in the center between the panels and it works well.</p>
  <p><a href="https://i.imgur.com/9yGs0Vc.mp4" rel="nofollow noreferrer">Here is a screencast</a> demonstrating the problem: you can see the scrollbar showing up every-time the Selected Viewer (panel on the right) is resized one pixel shorter in width compared to the panel on the left.</p>
  <p>Here is mostly what I'm doing:</p>
  <pre class="lang-py prettyprint-override"><code>       verticalLayout = QVBoxLayout(self)
         verticalLayout.setSpacing(0)
         verticalLayout.setContentsMargins(0, 0, 0, 0)
         gridLayout = QGridLayout()
         # Minimum width for the toolbar in the middle:
         gridLayout.setColumnMinimumWidth(1, 30)
         gridLayout.setColumnStretch(0,1)
         gridLayout.setColumnStretch(1,0)
         gridLayout.setColumnStretch(2,1)
         gridLayout.setSpacing(3)
      
         selectedImageViewer = ScrollAreaImageViewer(self)
         gridLayout.addWidget(selectedImageViewer, 0, 0, 3, 1)
  
         verticalToolBar = QToolBar(self)
         verticalToolBar.setOrientation(Qt.Orientation(Qt.Vertical))
  
         gridLayout.addWidget(verticalToolBar, 1, 1, 1, 1, Qt.AlignCenter)
  
         referenceImageViewer = ScrollAreaImageViewer(self)
         gridLayout.addWidget(referenceImageViewer, 0, 2, 3, 1)
  
         verticalLayout.addLayout(gridLayout)
  </code></pre>
  <p>I add another widget below in the QVBoxLayout but it's irrelevant here.
  I have tried adding spacers but it doesn't seem to change anything:</p>
  <pre class="lang-py prettyprint-override"><code>gridLayout.addItem(QSpacerItem(5,0, QSizePolicy.Minimum), 1, 3, 1, 1, Qt.Alignment(Qt.AlignCenter))
  </code></pre>
  <p>Is there a way to ensure both Viewers get the same size without using resize() on them on every resizeEvent()?
  Or should this actually be considered a bug in Qt?</p>
  <p>I have tried the following which works around the scrollbar flickering issue:</p>
  <pre class="lang-py prettyprint-override"><code>def resizeEvent(self, event):
      self.gridLayout.setColumnMinimumWidth(0, self.selectedImageViewer.size().width())
      self.gridLayout.setColumnMinimumWidth(2, self.selectedImageViewer.size().width())
  </code></pre>
  <p>But the sizes still differ by one pixel once out of twice.</p>
  <p>Edit: here is a minimal reproducible example</p>
  <pre class="lang-py prettyprint-override"><code>from PyQt5.QtCore import QSize, Qt
  from PyQt5.QtWidgets import (QDialog, QLayout, QVBoxLayout,
      QLabel, QSizePolicy, QToolBar, QGridLayout,
      QWidget, QApplication )
  
  class MyWidget(QWidget):
      def __init__(self, parent):
          super().__init__(parent)
          self.label = QLabel(self)
  
      def resizeEvent(self, event):
          self.label.setText(f&quot;{self.size()}&quot;)
  
  class MyDialog(QDialog):
      def __init__(self, parent):
          super().__init__(parent)
          self.setMinimumSize(QSize(500, 100))
          self.verticalLayout = QVBoxLayout(self)
          self.verticalLayout.setSpacing(0)
          self.verticalLayout.setContentsMargins(0, 0, 0, 0)
          self.gridLayout = QGridLayout()
          self.gridLayout.setColumnMinimumWidth(1, 30)
          self.gridLayout.setColumnStretch(0,1)
          self.gridLayout.setColumnStretch(1,0)
          self.gridLayout.setColumnStretch(2,1)
          self.gridLayout.setSpacing(3)
  
          self.selectedImageViewer = MyWidget(self)
          self.gridLayout.addWidget(self.selectedImageViewer, 0, 0, 3, 1)
          self.verticalToolBar = QToolBar(self)
          self.verticalToolBar.setOrientation(Qt.Orientation(Qt.Vertical))
          self.gridLayout.addWidget(self.verticalToolBar, 1, 1, 1, 1, Qt.AlignCenter)
          self.referenceImageViewer = MyWidget(self)
          self.gridLayout.addWidget(self.referenceImageViewer, 0, 2, 3, 1)
          self.verticalLayout.addLayout(self.gridLayout)
  
  def main():
      app = QApplication([()])
      window = QWidget()
      dialog = MyDialog(window)
      dialog.show()
      return app.exec()
  
  if __name__ == &quot;__main__&quot;:
      main()
  </code></pre>