PDA

View Full Version : QMainWindow minimum size is much larger than the centralwidget inside it.



erangre
14th April 2016, 01:06
I am new to this forum (and new to PyQt).

I have a QMainWindow which contains a QWidget (which is set to be the centralwidget, and it is the only widget there).
The Qwidget has many widgets and layouts inside it, and all within one QVBoxLayout which is set to be the layout of the widget.
One of the widgets is a QFrame which starts out hidden and contains some buttons and lists.

At the startup the size of everything is fine. The Window is the correct size for containing the visible items.
If I make the QFrame visible it expands the window vertically, which is what I wanted.
But when I hide it again, the window does not shrink back to the original size (I tried the policies of Preferred, Expanding, and of MinimumExpanding)
I can cause it to shrink a bit, by using the sizeHint of the Qwidget for both the QWidget and the QMainWindow.
Manually I can change it back to the original size when it is running.
I checked the sizeHint, and when hiding the QFrame it doesn't change for the QMainWindow (whereas, for the QWidget it does change to the correct value when the QFrame is hidden).
Could my QMainWindow contain anything else besides the QWidget somehow?
Or maybe sizehint for QMainWindow does not become smaller when possible? That still wouldn't explain why when I try to set the size of the QMainWindow within the code it doesn't work.

Thank you.

anda_skoa
14th April 2016, 09:26
It just doesn't automatically become smaller when you hide the frame.
Just resize it manually once after hiding.

Cheers,
_

erangre
14th April 2016, 15:39
It just doesn't automatically become smaller when you hide the frame.
Just resize it manually once after hiding.

Cheers,
_

Thank you for the response. I see I wasn't clear in my statement. By manually, I meant that I had to use the mouse to drag the window size, and I couldn't find any code that would make it smaller. Using the sizehint of the internal QWidget made it a bit smaller, but not nearly the correct size, even though the sizehint of the QWidget is the correct size.

Cheers!

anda_skoa
14th April 2016, 17:15
Ah, I see.

You can try to resize(0, 0)

Cheers,
_

erangre
14th April 2016, 17:42
Thanks. With 0, 0 it made it too small, but then I realized I could save the size at startup, and then when needed set the minimum size to the startup size and the size to the minimum size. I am not sure if this is the "correct" way to do it, but it works.

ChrisW67
15th April 2016, 22:51
If you want to track the size hint of the contained widget always then look at the Fixed policy on the main window and a SetFixedSize on the layout of the contained widget. The Extension Example in the Qt docs might also help.