<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[QLayout retroactive parenting?]]></title><description><![CDATA[<p dir="auto">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):</p>
<pre><code>QDialog* dlg { new QDialog() };
QVBoxLayout* layout { new QVBoxLayout(dlg) };
QPushButton* btn { new QPushButton("Click me!") };
layout-&gt;addWidget(btn); // dlg is set as parent of btn here
</code></pre>
<p dir="auto">But if the layout is created parentless, will it retroactively assign parentage once I add it to the Dialog?</p>
<pre><code>QVBoxLayout* layout { new QVBoxLayout() }; // note: Not assigned to any widget yet
QPushButton* btn { new QPushButton("Click me!") };
layout-&gt;addWidget(btn); // btn cannot be assigned a parent
...
QDialog* dlg { new QDialog() };
dlg-&gt;setLayout(layout); // will btn be retroactively made a child of dlg here?
</code></pre>
<p dir="auto"><a href="https://doc.qt.io/qt-6/layout.html" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-6/layout.html</a> mentions that</p>
<pre><code>Widgets can only have other widgets as parent, not layouts.
</code></pre>
<p dir="auto">If so, how would retroactive parenting even work?<br />
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.</p>
]]></description><link>https://forum.qt.io/topic/164623/qlayout-retroactive-parenting</link><generator>RSS for Node</generator><lastBuildDate>Fri, 18 Sep 2026 05:30:26 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/164623.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 30 Apr 2026 12:48:41 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QLayout retroactive parenting? on Thu, 30 Apr 2026 12:52:45 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/estelyen">@<bdi>Estelyen</bdi></a> said in <a href="/post/838049">QLayout retroactive parenting?</a>:</p>
<blockquote>
<p dir="auto">dlg-&gt;setLayout(layout); // will btn be retroactively made a child of dlg here?</p>
</blockquote>
<p dir="auto">So far as I know, yes.</p>
<p dir="auto">In any case, it's so easy to check e.g. <code>btn-&gt;parent()</code> after this, so if you're worried why not do so?</p>
]]></description><link>https://forum.qt.io/post/838050</link><guid isPermaLink="true">https://forum.qt.io/post/838050</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Thu, 30 Apr 2026 12:52:45 GMT</pubDate></item><item><title><![CDATA[Reply to QLayout retroactive parenting? on Thu, 30 Apr 2026 13:05:01 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> said in <a href="/post/838050">QLayout retroactive parenting?</a>:</p>
<blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/estelyen">@<bdi>Estelyen</bdi></a> said in <a href="/post/838049">QLayout retroactive parenting?</a>:</p>
<blockquote>
<p dir="auto">dlg-&gt;setLayout(layout); // will btn be retroactively made a child of dlg here?</p>
</blockquote>
<p dir="auto">So far as I know, yes.</p>
<p dir="auto">In any case, it's so easy to check e.g. <code>btn-&gt;parent()</code> after this, so if you're worried why not do so?</p>
</blockquote>
<p dir="auto">Okay, facepalm moment for me, I searched fo so long and never tried checking it myself. Need some sleep, apparently.<br />
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!</p>
]]></description><link>https://forum.qt.io/post/838052</link><guid isPermaLink="true">https://forum.qt.io/post/838052</guid><dc:creator><![CDATA[Estelyen]]></dc:creator><pubDate>Thu, 30 Apr 2026 13:05:01 GMT</pubDate></item><item><title><![CDATA[Reply to QLayout retroactive parenting? on Thu, 30 Apr 2026 12:52:45 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/estelyen">@<bdi>Estelyen</bdi></a> said in <a href="/post/838049">QLayout retroactive parenting?</a>:</p>
<blockquote>
<p dir="auto">dlg-&gt;setLayout(layout); // will btn be retroactively made a child of dlg here?</p>
</blockquote>
<p dir="auto">So far as I know, yes.</p>
<p dir="auto">In any case, it's so easy to check e.g. <code>btn-&gt;parent()</code> after this, so if you're worried why not do so?</p>
]]></description><link>https://forum.qt.io/post/838050</link><guid isPermaLink="true">https://forum.qt.io/post/838050</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Thu, 30 Apr 2026 12:52:45 GMT</pubDate></item></channel></rss>