3

I have a grid area in a scroll area within a dialog

class IndicSelectWindow(QDialog):
    def __init__(self, path, parent=None):
        super(IndicSelectWindow, self).__init__(parent)
        self.resize(500, 400)
        self.scroll_area = QScrollArea(self)
        self.scroll_area.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        self.grid_layout = QGridLayout(self.scroll_area)
        self.exec_()

How can I make the grid cover the full area of the scroll_area. it does not have a method setSizePolicy. How can I make this work?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
chrise
  • 4,039
  • 3
  • 39
  • 74

1 Answers1

8

You must add the QGridLayout to the QWidget that is added to the QScrollArea

import sys
from PyQt5 import QtWidgets


class IndicSelectWindow(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super(IndicSelectWindow, self).__init__(parent=parent)
        self.resize(500, 400)
        self.layout = QtWidgets.QHBoxLayout(self)
        self.scrollArea = QtWidgets.QScrollArea(self)
        self.scrollArea.setWidgetResizable(True)
        self.scrollAreaWidgetContents = QtWidgets.QWidget()
        self.gridLayout = QtWidgets.QGridLayout(self.scrollAreaWidgetContents)
        self.scrollArea.setWidget(self.scrollAreaWidgetContents)
        self.layout.addWidget(self.scrollArea)

        for i in range(100):
            for j in range(100):
                self.gridLayout.addWidget(QtWidgets.QPushButton(), i, j)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    w = IndicSelectWindow()
    w.show()
    sys.exit(app.exec_())

enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241