pyqt在滚动区域展开网格

pyqt expand grid in scroll area

我在对话框的滚动区域中有一个网格区域

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_()

如何让网格覆盖 scroll_area 的整个区域。它没有方法 setSizePolicy。我怎样才能完成这项工作?

您必须将 QGridLayout 添加到添加到 QScrollArea 的 QWidget

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_())