layout set and widget as parent...
-
Hello!
is this....
QWidget* wdg = new QWidget(this); QHBoxLayout* lay = new QHBoxLayout(wdg); //does internally applies layout to wdg parent? QPushButton* pbt = new QPushButton(wdg); //does internally layout applied to wdg add the button?
the same to this?
QWidget* wdg = new QWidget(this); QHBoxLayout* lay = new QHBoxLayout; wdg->setLayout(lay); QPushButton* pbt = new QPushButton; lay->addWidget(pbt);
-
@1XU7 said in layout set and widget as parent...:
i tought by setting wdg as parent to button, and wdg has lay as layout, the layout would add button automatically...
No and I don't see any evidence that a QObject-based parent-child relationship should have something to do with a layout.
The other way round is correct - when you add a widget to another widgets layout then the other widget is becoming the parent of the child because otherwise parent-child stuff like e.g. event propagation would not work. So no, no need to set a parent in your code but it will also not hurt and makes it more obvious what you're trying to do. -
According to the documentation, yes - at least for the QI misread your post - since you don't add pbt to a layout it will look different. Layouts and parent are two different concepts.
-
i tought by setting wdg as parent to button, and wdg has lay as layout, the layout would add button automatically... but you mean that is not made.. i need to manually add button to layout:
QWidget* wdg = new QWidget(this); QHBoxLayout* lay = new QHBoxLayout(wdg); QPushButton* pbt = new QPushButton(wdg); //it is necesary to set wdg as parent here? lay->addWidget(pbt);
-
@1XU7 said in layout set and widget as parent...:
i tought by setting wdg as parent to button, and wdg has lay as layout, the layout would add button automatically...
No and I don't see any evidence that a QObject-based parent-child relationship should have something to do with a layout.
The other way round is correct - when you add a widget to another widgets layout then the other widget is becoming the parent of the child because otherwise parent-child stuff like e.g. event propagation would not work. So no, no need to set a parent in your code but it will also not hurt and makes it more obvious what you're trying to do. -