1
votes

I have a QVBoxLayout which has a QSCrollArea and with addStretch() added to its end.

QScrollArea here gets to use only half of the available space eventhough there is free space below,, currently occupied by addStretch.

If contents of the QScrollArea are large enough, I want QScrollArea to expand to all the available space. When QScrollArea's contents are smaller, I want QScrollArea to shrink to that size, with rest of the available space used by addStetch.

However, currently, for small QScrollArea this works but when QScrollArea's contents are larger, it only expands to half of the available space.

I tried setting various Size Policies for the QScrollArea and the underlying scrolling widget but nothing works.

What's the trick to get this sorted?

Here's a snipped of my code [in Python but answers in C++ are very welcome]:

# My setup
self.layout = QVBoxLayout()
self.setLayout(self.layout)

scrollArea = QScrollArea()
self.layout.addWidget(scrollArea)
scrollArea.setWidgetResizable(True)
scrollArea.setEnabled(True) 

self.scrollWidget = QWidget()
self.scrollLayout = QVBoxLayout()
self.scrollWidget.setLayout(self.scrollLayout)

scrollArea.setWidget(self.scrollWidget)

# Now what I do is, I add bunch of widgets inside the self.scrollLayout
# and in the end, I perform a self.layout.addStretch() to push them up (verticle Alignment does same)
# Maximum height QScrollArea (scrollArea) gets is stuck at around 50% although I want to take all the space if necessary

I added ALL possible setSizePolicy(..) options

scrollArea.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding))
# and ALL others. None works. addStretch() still takes 50% minimum space and gets even more if QScrollArea's contents are smaller.
3

3 Answers

1
votes

I just had the very same problem and I think I found a solution. Do not add a stretch after the QScrollArea, but as last element inside the QScrollArea layout. So in your code, use self.scrollLayout.addStretch() instead of self.layout.addStretch().

1
votes

I found the solution. It's necessary to override sizeHint to holding scrollarea's scrolling widget. It's necessary to set maximum height to scrolling widget. And it's necessary to place this widget inside a QVBoxLayout with addStretch() which pushes it to the top.

0
votes

When QScrollArea's contents are smaller, I want QScrollArea to shrink to that size, with rest of the available space used by addStetch.

If contents of QScrollArea are small, you can align them to the top and hide vertical scrollbar. In this case you won't need to add stretch to the main vertical layout.

If contents are larger than QScrollArea, it will take all available space.

I think, this is what you need.