QScrollArea is not resizing as expected
-
When I use QScrollArea in layout with Stretch, after resizing I'll get the big blank area where stretch is added.
That's a minimal example that show my problemfrom PySide2 import QtCore, QtWidgets, QtGui class BugWithScrollareaAndStretch(QtWidgets.QWidget): def __init__(self): super(BugWithScrollareaAndStretch, self).__init__() scrollarea = QtWidgets.QScrollArea() scrollarea.setWidgetResizable(True) scrollareaLayout = QtWidgets.QVBoxLayout() for i in range(20): scrollareaLayout.addWidget(QtWidgets.QPushButton(f'Button # {i}')) scrollareaWidget = QtWidgets.QWidget() scrollareaWidget.setLayout(scrollareaLayout) scrollarea.setWidget(scrollareaWidget) bottomButton = QtWidgets.QPushButton() mainLay = QtWidgets.QVBoxLayout() mainLay.addWidget(scrollarea) # Scrollarea is not resizing properly with the .addStretch() mainLay.addStretch() mainLay.addWidget(bottomButton) self.setLayout(mainLay) app = QtWidgets.QApplication([]) widget = BugWithScrollareaAndStretch() widget.show() app.exec_()
This is how it looks in action:
-
@Artofka said in QScrollArea is not resizing as expected:
@JonB Thanks for responce. I was expected from QScrollArea to fill all the area untill bottom button
mainLay.addStretch()
The word stretch is confusing here, because it doesn't concern the scrollarea but the spacer that was inserted.
The main goal of a strech spacer is to force all the widgets (here your buttons) to their minimum height.You can experiment these with the Designer. Great tool to understand how layout, spacer, etc, work together.
-
@Artofka
I'm not the expert, I just play with Qt's layout stuff till it does what I want/I beat it into submission! ;-) But the point of a stretch is to fill/take up space (it's a QSpacerItem, "The QSpacerItem class provides blank space in a layout"), normally as much as possible. It stretches to consume the available space. It's used to push the following thing right horizontally or down vertically, just as you show here.So I guess you want "QScrollArea to fill all the area untill bottom button", i.e. be as tall as possible, then start scrolling? Did you try it without adding that stretch?
I hope I have not told you the wrong thing! You may have to wait for a more knowledgeable person than I!
-
@Artofka
I think somebody else should tell you the right way to do that!I will comment on one thing. Although you can use visible or adding/removing to toggle which widget is visible, normally the most convenient is to put the alternatives in a QStackedWidget, q.v. However, I know/found that a stacked widget usually takes up the amount of space of the largest of its alternatives. Maybe that would help here, maybe not, but it's worth knowing about that widget.
-
Hi,
From the looks of it, you should check QStackedWidget to handle your multiple widgets.