In the heir class, QWidget added the following field:

QPushButton *m_sendButton = new QPushButton("Отправить", this); 
I didn't add this button to any layout! If you pass this to the second pameter, then it appears on the form in the upper left corner, if not, then not. Is this normal behavior and widgets automatically fall into the layout of the parent?

    2 answers 2

    Yes, this is normal behavior . And they fall not on the layout, but on the QWidget (the parent, the one that this ).

    If parent is 0, the new widget becomes a window. If parent is another widget, this widget becomes a child window inside parent .

    Layout itself assigns the parent to widgets, and itself only controls their positioning.

    When constructing the child widgets. The layout will automatically replicate the widgets (using QWidget :: setParent ()).

    How to correctly write out here .

    Example:

     QWidget *window = new QWidget; QPushButton *button1 = new QPushButton("One"); QLineEdit *lineEdit1 = new QLineEdit(); QPushButton *button2 = new QPushButton("Two"); QLineEdit *lineEdit2 = new QLineEdit(); QPushButton *button3 = new QPushButton("Three"); QLineEdit *lineEdit3 = new QLineEdit(); QFormLayout *layout = new QFormLayout; layout->addRow(button1, lineEdit1); layout->addRow(button2, lineEdit2); layout->addRow(button3, lineEdit3); window->setLayout(layout); window->show(); 
    • Clear. With no habits somewhat unexpected ... - cipher_web

    QLayout is just an auxiliary class that defines the position and size of slave widgets based on the geometry of their parent and neighbors.

    In the case of adding widgets, the direct task of the parent (via setParent() ), bypassing the layout, no one controls their geometry and they are positioned solely by manually changing their position ( move() ) and size ( resize() ) by the program.

    • I have been developing GUI for half a year. It always turned out that when starting the widget immediately added it to a specific layout and everything was fine. And then some garbage. I just did not notice the announcement of this widgeta in a pile of fields of its class and the appearance of the button which was not strange. Thanks for the support! - cipher_web