I have a QGroupBox
containing multiple tables.
At some point, the cummulative tables height exceed the screen height so they get squashed, and a scrollarea is added to each.
I want to have the tables keep their natural size, and make the entire QGroupBox
scrollable.
Tried the following: PyQt: How to make widget scrollable. It did not help. The scrollarea does not appear, it seems the Qt logic always prefers to squash the tables instead of expanding the QGroupBox
.
I can make it appear by adding something like myGroupBox.setMinimumHeight(3000)
. But this, other than being a ugly patch, also deforms the tables, making them stretch beyond their natural size, and leaving a blank space at the bottom.
How can I fix this? Thanks. I am using Python3.9, PyQt5 over Windows.
Code example:
import random
from PyQt5 import QtWidgets
class SomeTable(QtWidgets.QTableWidget):
def __init__(self):
super().__init__()
header = ['A', 'B', 'C', 'D', 'E', 'F']
self.setColumnCount(len(header))
self.setHorizontalHeaderLabels(header)
for i in range(5):
self.insertRow(0)
for i in range(5):
for j in range(6):
self.setItem(i, j, QtWidgets.QTableWidgetItem(str(random.randint(0, 100))))
class Window(QtWidgets.QWidget):
def __init__(self):
super().__init__()
mygroupbox = QtWidgets.QGroupBox('this is my groupbox')
myform = QtWidgets.QVBoxLayout()
for i in range(10):
myform.addWidget(SomeTable())
mygroupbox.setLayout(myform)
scroll = QtWidgets.QScrollArea()
scroll.setWidget(mygroupbox)
scroll.setWidgetResizable(True)
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(scroll)
if __name__ == '__main__':
app = QtWidgets.QApplication(['Test'])
window = Window()
window.showMaximized()
app.exec_()