Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 868
sashoalm Avatar asked Jan 20 '23 08:01

sashoalm


2 Answers

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).

like image 79
docsteer Avatar answered Jan 21 '23 23:01

docsteer


I saw another way in one of the examples in a book called "C++ GUI Programming with Qt 4". They did it with

    setFixedHeight(sizeHint().height());
like image 26
sashoalm Avatar answered Jan 21 '23 23:01

sashoalm