<?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[[SOLVED] Am I deleating these variables right?]]></title><description><![CDATA[<p dir="auto">just wondering if I was deallocating these variables properly:</p>
<p dir="auto">' ' '<br />
int main(int argc, char *argv[])<br />
{<br />
QApplication a(argc, argv);<br />
QWidget *window = new QWidget;</p>
<pre><code>QSplitter splitter;

window-&gt;setWindowTitle("test");
checkbox *box = new checkbox;

QHBoxLayout *mainLayout = new QHBoxLayout;

splitter.addWidget(box);
splitter.addWidget(window);

window-&gt;setLayout(mainLayout);
window-&gt;show();



return a.exec();
</code></pre>
<p dir="auto">}<br />
' ' '</p>
]]></description><link>https://forum.qt.io/topic/57677/solved-am-i-deleating-these-variables-right</link><generator>RSS for Node</generator><lastBuildDate>Tue, 04 Aug 2026 16:56:13 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/57677.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 12 Aug 2015 03:59:01 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [SOLVED] Am I deleating these variables right? on Thu, 13 Aug 2015 01:37:50 GMT]]></title><description><![CDATA[<p dir="auto">thanks for the reply, will note that down</p>
]]></description><link>https://forum.qt.io/post/286272</link><guid isPermaLink="true">https://forum.qt.io/post/286272</guid><dc:creator><![CDATA[manny]]></dc:creator><pubDate>Thu, 13 Aug 2015 01:37:50 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Am I deleating these variables right? on Wed, 12 Aug 2015 16:05:56 GMT]]></title><description><![CDATA[<p dir="auto">Although the answers given here were correct it is worth noting that the whole topic could have been avoided by not allocation the window on the heap. If the app has a single main widget it's simpler to just allocate it on the stack and don't fiddle with <code>aboutToQuit</code>:</p>
<pre><code class="language-cpp">int main(int argc, char *argv[])
{
   QApplication a(argc, argv);

   QWidget window;
   window.setWindowTitle("test");
   window.show();

   return a.exec();
}
</code></pre>
<p dir="auto">Also, if a widget is created on the heap and you want it deleted when it is closed, the simplest way is to set appropriate attribute:</p>
<pre><code class="language-cpp">QWidget* w= new QWidget();
w-&gt;setAttribute(Qt::WA_DeleteOnClose); //this will call deleteLater() when widget is closed.
w-&gt;show();
</code></pre>
]]></description><link>https://forum.qt.io/post/286232</link><guid isPermaLink="true">https://forum.qt.io/post/286232</guid><dc:creator><![CDATA[Chris Kawa]]></dc:creator><pubDate>Wed, 12 Aug 2015 16:05:56 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Am I deleating these variables right? on Wed, 12 Aug 2015 15:38:49 GMT]]></title><description><![CDATA[<p dir="auto">ok, i think i understand now. Thanks for the help, really appreciate it.</p>
]]></description><link>https://forum.qt.io/post/286229</link><guid isPermaLink="true">https://forum.qt.io/post/286229</guid><dc:creator><![CDATA[manny]]></dc:creator><pubDate>Wed, 12 Aug 2015 15:38:49 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Am I deleating these variables right? on Wed, 12 Aug 2015 15:10:31 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/manny">@<bdi>manny</bdi></a> said:</p>
<blockquote>
<p dir="auto">so basically if I make a new instance of a class like</p>
<p dir="auto">' className object  = new className;<br />
object-&gt;show();</p>
<p dir="auto">it will be deleted once className is desetroyed</p>
</blockquote>
<p dir="auto">Not exactly: every QWidget destroys its children. The deleteLater trick allows to destroy the top-level widget when the application quits.</p>
]]></description><link>https://forum.qt.io/post/286227</link><guid isPermaLink="true">https://forum.qt.io/post/286227</guid><dc:creator><![CDATA[JohanSolo]]></dc:creator><pubDate>Wed, 12 Aug 2015 15:10:31 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Am I deleating these variables right? on Wed, 12 Aug 2015 14:47:50 GMT]]></title><description><![CDATA[<p dir="auto">so basically if I make a new instance of a class like</p>
<p dir="auto">' className object  = new className;<br />
object-&gt;show();</p>
<p dir="auto">it will be deleted once className is desetroyed</p>
]]></description><link>https://forum.qt.io/post/286224</link><guid isPermaLink="true">https://forum.qt.io/post/286224</guid><dc:creator><![CDATA[manny]]></dc:creator><pubDate>Wed, 12 Aug 2015 14:47:50 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Am I deleating these variables right? on Wed, 12 Aug 2015 14:32:48 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/manny">@<bdi>manny</bdi></a> said:</p>
<blockquote>
<p dir="auto">but if i were to, say, make a program in which the widgets are added to the layout in the class constructor, should I use the the splitter technique to destroy all the instances of the class that I made?</p>
</blockquote>
<p dir="auto">All the objects (deriving from QObject) you allocate and add to the layout of a widget will be destroyed when the widget itself is destroyed.</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/manny">@<bdi>manny</bdi></a> said:</p>
<blockquote>
<p dir="auto">also if I need to delete window, should i just add it to splitter so that when splitter is deleted (it is a local variable) window will subsequently be deleted as well?</p>
</blockquote>
<p dir="auto">If I'm not mistaken, you can rely on <a href="http://doc.qt.io/qt-5/qcoreapplication.html#signals" target="_blank" rel="noopener noreferrer nofollow ugc">QApplication::aboutToQuit()</a> to destroy your widget:</p>
<pre><code class="language-cpp">connect( &amp;a, SIGNAL( aboutToQuit() ), window, SLOT( deleteLater() ) );
</code></pre>
<p dir="auto">See <a href="http://doc.qt.io/qt-5/qobject.html#deleteLater" target="_blank" rel="noopener noreferrer nofollow ugc">the doc</a> for detailed information.</p>
]]></description><link>https://forum.qt.io/post/286223</link><guid isPermaLink="true">https://forum.qt.io/post/286223</guid><dc:creator><![CDATA[JohanSolo]]></dc:creator><pubDate>Wed, 12 Aug 2015 14:32:48 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Am I deleating these variables right? on Wed, 12 Aug 2015 13:49:07 GMT]]></title><description><![CDATA[<p dir="auto">but if i were to, say, make a program in which the widgets are added to the layout in the class constructor, should I use the the splitter technique to destroy all the instances of the class that I made?</p>
<p dir="auto">also if I need to delete window, should i just add it to splitter so that when splitter is deleted (it is a local variable) window will subsequently be deleted as well?</p>
]]></description><link>https://forum.qt.io/post/286214</link><guid isPermaLink="true">https://forum.qt.io/post/286214</guid><dc:creator><![CDATA[manny]]></dc:creator><pubDate>Wed, 12 Aug 2015 13:49:07 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Am I deleating these variables right? on Wed, 12 Aug 2015 13:37:20 GMT]]></title><description><![CDATA[<p dir="auto">Thanks for the replies, i'll check out those docs</p>
]]></description><link>https://forum.qt.io/post/286213</link><guid isPermaLink="true">https://forum.qt.io/post/286213</guid><dc:creator><![CDATA[manny]]></dc:creator><pubDate>Wed, 12 Aug 2015 13:37:20 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Am I deleating these variables right? on Wed, 12 Aug 2015 07:27:23 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mrjj">@<bdi>mrjj</bdi></a> said:</p>
<blockquote>
<p dir="auto">So in this case, all he needs is<br />
delete window;</p>
</blockquote>
<p dir="auto">I think so, yes. This reparenting is explaing in the <a href="http://doc.qt.io/qt-5/layout.html" target="_blank" rel="noopener noreferrer nofollow ugc">doc</a> (Qt 5.5) or <a href="http://doc.qt.io/qt-4.8/layout.html" target="_blank" rel="noopener noreferrer nofollow ugc">there</a> for Qt 4.8. Look at the `Tips for Using Layouts' section.</p>
]]></description><link>https://forum.qt.io/post/286161</link><guid isPermaLink="true">https://forum.qt.io/post/286161</guid><dc:creator><![CDATA[JohanSolo]]></dc:creator><pubDate>Wed, 12 Aug 2015 07:27:23 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Am I deleating these variables right? on Wed, 12 Aug 2015 07:19:19 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/johansolo">@<bdi>JohanSolo</bdi></a><br />
Ahh, I did not know using a layout would reparent.</p>
<p dir="auto">So in this case, all he needs is<br />
delete window;</p>
]]></description><link>https://forum.qt.io/post/286159</link><guid isPermaLink="true">https://forum.qt.io/post/286159</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Wed, 12 Aug 2015 07:19:19 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Am I deleating these variables right? on Wed, 12 Aug 2015 07:15:29 GMT]]></title><description><![CDATA[<blockquote>
<p dir="auto">As far as I know, the Layout will not delete objects</p>
</blockquote>
<p dir="auto">The layout won't, but the Window will.</p>
<blockquote>
<p dir="auto">Also if you use window for checkbox<br />
checkbox *box = new checkbox(Window);<br />
It will be owned by window and deleted with it.</p>
</blockquote>
<p dir="auto">A widget put in a layout which is then used in another widget has its parent reassigned. Therefore the QCheckBox will indeed be deleted <em>at the time the Window is</em>. I consider a good practice to set the parent anyway, I don't think it hurts.</p>
]]></description><link>https://forum.qt.io/post/286157</link><guid isPermaLink="true">https://forum.qt.io/post/286157</guid><dc:creator><![CDATA[JohanSolo]]></dc:creator><pubDate>Wed, 12 Aug 2015 07:15:29 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Am I deleating these variables right? on Wed, 12 Aug 2015 06:41:12 GMT]]></title><description><![CDATA[<p dir="auto">Hi  And welcome</p>
<p dir="auto">As far as I know, the Layout will not delete objects<br />
And since you NEW "window," i do not think it get deleted.<br />
You do not need to new QWidget.<br />
Can just be<br />
QWidget window;<br />
In which case it will be deleted when main ends.<br />
Also if you use window for checkbox<br />
checkbox *box = new checkbox(Window);<br />
It will be owned by window and deleted with it.</p>
]]></description><link>https://forum.qt.io/post/286150</link><guid isPermaLink="true">https://forum.qt.io/post/286150</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Wed, 12 Aug 2015 06:41:12 GMT</pubDate></item></channel></rss>