QLayout retroactive parenting?
-
I know that adding a Widget (let's say a QPushButton) to a QLayout will automatically set its parent to the Widget the layout is installed in (let's say a QDialog):
QDialog* dlg { new QDialog() }; QVBoxLayout* layout { new QVBoxLayout(dlg) }; QPushButton* btn { new QPushButton("Click me!") }; layout->addWidget(btn); // dlg is set as parent of btn hereBut if the layout is created parentless, will it retroactively assign parentage once I add it to the Dialog?
QVBoxLayout* layout { new QVBoxLayout() }; // note: Not assigned to any widget yet QPushButton* btn { new QPushButton("Click me!") }; layout->addWidget(btn); // btn cannot be assigned a parent ... QDialog* dlg { new QDialog() }; dlg->setLayout(layout); // will btn be retroactively made a child of dlg here?https://doc.qt.io/qt-6/layout.html mentions that
Widgets can only have other widgets as parent, not layouts.If so, how would retroactive parenting even work?
Already searched the documentation as well as the forums and even google, but couldn't find any answer to this. So I'm asking it here since I'm worried it might create huge memory leaks when my dialog won't clean up the widgets in it once it goes out of scope. -
I know that adding a Widget (let's say a QPushButton) to a QLayout will automatically set its parent to the Widget the layout is installed in (let's say a QDialog):
QDialog* dlg { new QDialog() }; QVBoxLayout* layout { new QVBoxLayout(dlg) }; QPushButton* btn { new QPushButton("Click me!") }; layout->addWidget(btn); // dlg is set as parent of btn hereBut if the layout is created parentless, will it retroactively assign parentage once I add it to the Dialog?
QVBoxLayout* layout { new QVBoxLayout() }; // note: Not assigned to any widget yet QPushButton* btn { new QPushButton("Click me!") }; layout->addWidget(btn); // btn cannot be assigned a parent ... QDialog* dlg { new QDialog() }; dlg->setLayout(layout); // will btn be retroactively made a child of dlg here?https://doc.qt.io/qt-6/layout.html mentions that
Widgets can only have other widgets as parent, not layouts.If so, how would retroactive parenting even work?
Already searched the documentation as well as the forums and even google, but couldn't find any answer to this. So I'm asking it here since I'm worried it might create huge memory leaks when my dialog won't clean up the widgets in it once it goes out of scope.@Estelyen said in QLayout retroactive parenting?:
dlg->setLayout(layout); // will btn be retroactively made a child of dlg here?
So far as I know, yes.
In any case, it's so easy to check e.g.
btn->parent()after this, so if you're worried why not do so? -
@Estelyen said in QLayout retroactive parenting?:
dlg->setLayout(layout); // will btn be retroactively made a child of dlg here?
So far as I know, yes.
In any case, it's so easy to check e.g.
btn->parent()after this, so if you're worried why not do so?@JonB said in QLayout retroactive parenting?:
@Estelyen said in QLayout retroactive parenting?:
dlg->setLayout(layout); // will btn be retroactively made a child of dlg here?
So far as I know, yes.
In any case, it's so easy to check e.g.
btn->parent()after this, so if you're worried why not do so?Okay, facepalm moment for me, I searched fo so long and never tried checking it myself. Need some sleep, apparently.
Anyways, I just did a test and it indeed assigned dlg as parent of btn. At least the memory addresses are the same. Thanks for the help! -
E Estelyen has marked this topic as solved