gpt4 book ai didi

python - pyqt在滚动区域展开网格

转载 作者:行者123 更新时间:2023-11-28 22:31:20 31 4
gpt4 key购买 nike

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

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

如何让网格覆盖滚动区域的整个区域。它没有方法 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_())

enter image description here

关于python - pyqt在滚动区域展开网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41616864/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com