具有不可拉伸高度行的 GridLayout
QGridlLayout with non-stretchable-height rows
我正在尝试构建一个具有可拉伸宽度列但不可拉伸高度行的 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)
Sets the stretch factor of row row to stretch. The first row is number 0.
The stretch factor is relative to the other rows in this grid. Rows with a higher stretch factor take more of the available space.
是的,是不是因为必须在添加完所有行后才调用它。
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_())
我正在尝试构建一个具有可拉伸宽度列但不可拉伸高度行的 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)
Sets the stretch factor of row row to stretch. The first row is number 0.
The stretch factor is relative to the other rows in this grid. Rows with a higher stretch factor take more of the available space.
是的,是不是因为必须在添加完所有行后才调用它。
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_())