Why is preferred size ignored in QMainWindow (Qt5)?
-
I created a simple default project from QtCreator (without a form).
Then I change just QMainWindow. I set the size policy to minimum (vertical & horizonal).
Then I added a QLineEdit to the window, and set its vertical policy to minimum.
So the question is, why does the code below show a window that's taller than the edit bar?

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { QSizePolicy policy = QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); setSizePolicy(policy); QWidget *widget = new QWidget(); QVBoxLayout *layout = new QVBoxLayout(); QLineEdit *edit = new QLineEdit; policy = QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); edit->setSizePolicy(policy); edit->setGeometry(0,0, 96,56); layout->addWidget(edit); widget->setLayout(layout); setCentralWidget(widget); }
What am I missing? Why doesn't it look like this below ? :
Of course, if I used QWidget instead of QMainWindow, it would look fine, but what do I need to set in QMainWindow for it not to pad out space vertically around the label?
Alternatively, I just add the callresize(sizeHint());
inside QMainWindow's constructor (at the end)?
This works, but it seems a bit clumsy. -
Hello,
I'm not sure these are the best practises but please try to:
- Remove the calls related to the size policy. The window will be taking the less needed space, by default, If I'm not mistaken.
- And, or, remove the explicit call to setGeometry() to let default values being applied;
- And, or, call adjustSize() just before calling show().