I've had a real challenge getting QScrollArea
to take the minimum space possible up to a maximum height.
My GUI model is as follows: A QScrollArea
contains a vertical layout which is populated with a widget of class TableRow
. I want this class TableRow
to take up the minimum height possible. It has a widget at the top which is always visible, and a QScrollArea
below which has a label inside it whose visibility can be toggled. The label is for notes which may be 0 characters or may be infinite in length (hardware limitations aside).
I've found that for a label in class TableRow
setting the vertical sizePolicy
to Fixed
will actually take up exactly how much it needs to fit all the contents (see: Qt Layout, resize to minimum after widget size changes). However this doesn't appear to work with QScrollArea
. In fact every sizePolicy
I've tried keeps the QScrollArea
at a fixed height; except for Ignore
, but then the QScrollArea
goes to a height of 0, regardless of its contents.
I've created a git branch producing a simplified version of this problem.
Here is the result of applying a fixed vertical sizePolicy:
What I'm expecting from this test case:
- The first widget's height should be almost 30px (the height of the upper widget) only showing the borders for the
QLabel
andQScrollArea
- The second widget's height should be shorter than 130px (the maximum height of the
QScrollArea
being 100px) but large enough to show the label without scrolling - The third widget's height should be 130px, and the scrollbar should appear (this part is correct in every case I've tried except for when the vertical
sizePolicy
is set toIgnored
)
I understand I may need to override some things to make this work, as by itself it's not obvious why a QScrollArea's height might be dependent on its child widgets (which is probably why it was not designed to make this easy, or at least it seems like it wasn't).
However, I think the case I'm trying to make is common enough, and my current approach is justifiable. If there's another/better way to make an individual widget scroll after it reaches a maximum height I'm open to that as an answer, provided it meets the three conditions I'm expecting.