Pressing the button calls the function:

void MainWindow::MenuN_1() { QWidget *widget = new QWidget(); widget->setGeometry(QRect(0, 0, 800, 250)); widget->resize(200,300); QFormLayout *form = new QFormLayout(widget); n1le_number1 = new QLineEdit; n1le_number2 = new QLineEdit; n1l_answer = new QLabel(); QPushButton *pb_check = new QPushButton("Сравнить"); form->setWidget(0, QFormLayout::FieldRole, n1le_number1); form->setWidget(1, QFormLayout::FieldRole, n1le_number2); form->setWidget(3, QFormLayout::FieldRole, n1l_answer); form->setWidget(2, QFormLayout::FieldRole, pb_check); QPushButton *cancel = new QPushButton("Назад"); form->setWidget(4, QFormLayout::FieldRole, cancel); setCentralWidget(widget); connect(cancel, SIGNAL(clicked(bool)), this, SLOT(Menu1())); connect(pb_check, SIGNAL (clicked()), this, SLOT (pb_pressedN1())); } 

The question is why neither setGeometry nor resize affect the size and position of the window?

I would like the window to have a minimum size (fully visible widgets, but no empty space)

  • If you just need a fixed size set, then there is a setFixedSize method. But with layouts, it's probably better. - Unick

1 answer 1

The fact is that after a widget becomes a child of the MainWindow , it stops controlling its size and position, and you need to set the size of the MainWindow itself. That is, setGeometry and resize affect only top-level widgets.

There are also tools to control the placement of widgets inside Layouts. A widget can specify its minimum and maximum acceptable sizes, and how to use them. See the layout documentation: http://doc.qt.io/qt-5/layout.html#adding-widgets-to-a-layout