You can resize the window to minimumSizeHint() after hiding the widget:
self.resize(minimumSizeHint())
This will reduce the window size to a minimum size.
If you only want to reduce the height, you can do something like:
self.resize(width(), minimumSizeHint().height())
But you should keep in mind that the minimum size is not calculated until some events are processed in the event loop. So when you hide your widget, just handle the event loop for some iterations, and then resize to a minimum.
Like it:
for i in range(0, 10): QApplication.processEvents() self.resize(width(), minimumSizeHint().height())
Another option is a single QTimer snapshot, which triggers a slot in which you resize the window to a minimum. Thus, when the window is resized, the minimum size hint is calculated correctly.
source share