我正在尝试构建具有可伸缩宽度列但非可伸缩高度行的QGridLayout。网格位于QScrollArea内部,除了高度之外,几乎都可以使用。您可以在以下图像中看到它:
如您所见,行被垂直拉伸。如果行太少(第一张图片),我希望所有行都相等,并且不适合所有父级的高度。我应该触摸网格还是实际的小部件?
编辑:可复制的示例
import sys
from PyQt5.QtWidgets import (QWidget, QGridLayout, QLabel, QRadioButton, QApplication, QScrollArea, QVBoxLayout)
class ScrollableGrid(QWidget):
def __init__(self, columnSpans, minimumColumnWidth):
super().__init__()
# Grid
self.grid = QWidget()
self.gridLayout = QGridLayout()
for i in range(len(columnSpans)):
self.gridLayout.setColumnStretch(i, columnSpans[i])
self.gridLayout.setColumnMinimumWidth(i, columnSpans[i] * minimumColumnWidth)
self.grid.setLayout(self.gridLayout)
# Scroll area
self.scrollArea = QScrollArea()
self.scrollArea.setWidget(self.grid)
self.scrollArea.setWidgetResizable(True)
# Compute the correct minimum width
width = (self.grid.sizeHint().width() +
self.scrollArea.verticalScrollBar().sizeHint().width() +
self.scrollArea.frameWidth() * 2)
self.scrollArea.setMinimumWidth(width)
# Layout
self.layout = QVBoxLayout()
self.layout.addWidget(self.scrollArea)
self.setLayout(self.layout)
def addRow(self, row, elements):
for column in range(len(elements)):
self.gridLayout.addWidget(elements[column], row, column)
class MainWindow(QWidget):
def __init__(self):
super().__init__()
# ScrollableGrid
self.grid = ScrollableGrid(columnSpans=[1,2,3], minimumColumnWidth=100)
# Add rows
for i in range(3):
self.grid.addRow(i, [QLabel('A'), QLabel('B'), QRadioButton()])
# Window layout
self.layout = QVBoxLayout()
self.layout.addWidget(self.grid)
self.setLayout(self.layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
windowExample = MainWindow()
windowExample.show()
sys.exit(app.exec_())
void QGridLayout :: setRowStretch(int row,int Stretch)
设置要拉伸的行的拉伸因子。第一行是数字0。
拉伸因子相对于此网格中的其他行。具有较高拉伸因子的行会占用更多可用空间。
是的,是因为添加完所有行后必须调用它吗?
import sys
from PyQt5.QtWidgets import (QWidget, QGridLayout, QLabel, QRadioButton,
QApplication, QScrollArea, QVBoxLayout)
class ScrollableGrid(QWidget):
def __init__(self, columnSpans, minimumColumnWidth):
super().__init__()
# Grid
self.grid = QWidget()
self.gridLayout = QGridLayout()
for i in range(len(columnSpans)):
self.gridLayout.setColumnStretch(i, columnSpans[i])
self.gridLayout.setColumnMinimumWidth(i, columnSpans[i] * minimumColumnWidth)
self.grid.setLayout(self.gridLayout)
# Scroll area
self.scrollArea = QScrollArea()
self.scrollArea.setWidget(self.grid)
self.scrollArea.setWidgetResizable(True)
# Compute the correct minimum width
width = (self.grid.sizeHint().width() +
self.scrollArea.verticalScrollBar().sizeHint().width() +
self.scrollArea.frameWidth() * 2)
self.scrollArea.setMinimumWidth(width)
# Layout
self.layout = QVBoxLayout()
self.layout.addWidget(self.scrollArea)
self.setLayout(self.layout)
def addRow(self, row, elements):
for column in range(len(elements)):
self.gridLayout.addWidget(elements[column], row, column)
class MainWindow(QWidget):
def __init__(self):
super().__init__()
# ScrollableGrid
self.grid = ScrollableGrid(columnSpans=[1,2,3], minimumColumnWidth=100)
# Add rows
for i in range(3):
self.grid.addRow(i, [QLabel('A'), QLabel('B'), QRadioButton()])
self.grid.gridLayout.setRowStretch(111, 1) # !!!
# Window layout
self.layout = QVBoxLayout()
self.layout.addWidget(self.grid)
self.setLayout(self.layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
windowExample = MainWindow()
windowExample.show()
sys.exit(app.exec_())