I've got a Qt app with a window that contains a QScrollArea
(plus a few small fixed-size controls around the borders, but window is mainly taken up by the QScrollArea
).
Inside the QScrollArea
is a fixed-size view of various widgets that the user can scroll across (using the scroll bars) when the window has been sized too small to fit the entire view.
There are some "Show Foo" and "Hide Foo" type menu items that allow the user to customize (to some extent) what widgets are visible inside the fixed-sized view, and when the user selects one of these menu items, the size of the fixed-sized view changes as the corresponding widgets are hidden or shown.
This all works okay; however I'd like to have the maximum-size of the window always be set to just large enough to allow the user to see the entire fixed-sized view without scrolling; there's no point in allowing the user to resize the window any larger than that, since the extra space would just be empty and useless.
I currently implement this behavior by manually calculating the maximum-window-size based on the current size of the fixed-size view and calling setMaximumSize()
on the window. However, this technique seems very fragile, since it depends not only on calculating the maximum size correctly (which is doable) but also calculating it at the correct time (i.e. calculate it too soon and the resulting size might not include all the new widgets yet, resulting in the maximum-window-size getting set too small).
That makes me wonder, is there some better way to achieve this result? This seems like it might be a fairly common use-case in Qt GUIs that use QScrollArea
.