Make a QMainWindow only horizontally-resizable - that is, width is resizable, but height is fixed

sashoalm picture sashoalm · Jun 29, 2011 · Viewed 9k times · Source

Can I make a QMainWindow with a grid layout resize only horizontally but not vertically?

I want its vertical size to be the minimum needed to hold all the buttons/line edits.

Answer

docsteer picture docsteer · Jun 29, 2011

Yes you can. As a QMainWindow inherits from QWidget, use the QWidget size policy settings to only allow resizing in the horizontal direction.

If working in Qt Designer, set the vertical size policy to be fixed, and the minimum height to be your desired height. In code:

QMainWindow *mainWindow = new QMainWindow();
mainWindow->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

Be cautious using absolute fixed size as the controls might need to still grow vertically (e.g. the user sets a high DPI font on their desktop).