I have a QScrollArea with a widget containing a QTableView. When other widgets in the QScrollArea are resized (enlarged) the QScrollArea should expand to accommodate the larger size. But instead, the QScrollArea sizing remains static, and the QTableView is contracted.
This behaviour ONLY occurs when there is a QTableView (or QTableWidget) in the QScrollArea widget - I can't reproduce it when other widget types are used. And in more inexplicable (to me!) behaviour, when the second widget's contents are set to hidden on startup, the issue disappears and QScrollArea behaves as exptected (see commented lines in code below)
Image before resizing:
Image showing unwanted resizing behaviour:
Image indicatively showing wanted resizing behaviour. QScrollArea scroll bar triggers to accommodate additional bottom widget space (off the bottom of the scroll bar area). In this example the Qtableview is still being truncated, and the QScrollArea is not actually expanding, but visually it indicates the kind of behaviour wanted:
Please can anyone help suggest what might be going on?
The code below is a minimum reproducible example
from PySide6.QtWidgets import (QVBoxLayout, QPushButton, QWidget, QLabel,QScrollArea,
QApplication, QTableWidget, QTableWidgetItem)
class WidgetWithTable(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
# table widget
table_widget = QTableWidget(5, 2)
for i in range(5):
for j in range(2):
item = QTableWidgetItem(f"{i + 1}-{j + 1}")
table_widget.setItem(i, j, item)
# another_widget
another_widget = QPushButton('Change the Scroll Area Size')
another_widget.clicked.connect(self.toggle_size)
# a widget to force resizing
self.resizing_label = QLabel('\n\nClicking the button should expand the QScrollArea, not contract the QTableView. If this label is set hidden at the start, the desired behaviour occurs.\n\n')
self.resizing_label.setWordWrap(True)
for w in [another_widget, self.resizing_label]:
w.setMaximumWidth(300)
# hiding the label on start up achieves desired behaviour;
# leaving the label shown reproduces the unwanted behaviour.
# self.resizing_label.hide()
main_layout = QVBoxLayout()
main_layout.addWidget(table_widget)
main_layout.addWidget(another_widget)
main_layout.addWidget(self.resizing_label)
self.setLayout(main_layout)
def toggle_size(self):
self.resizing_label.setVisible(not self.resizing_label.isVisible())
if __name__ == '__main__':
app = QApplication()
test_widget = WidgetWithTable()
scroll_widget = QScrollArea()
scroll_widget.setWidgetResizable(True)
scroll_widget.setWidget(test_widget)
scroll_widget.show()
app.exec()


