<?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[Error with unique_ptr]]></title><description><![CDATA[<p dir="auto">I can not find my mistake in using unique_ptr</p>
<p dir="auto">@/<em>Class Load .ui files hpp</em>/</p>
<p dir="auto">#ifndef LOADUI_H //if LoadUi.hpp not includ<br />
#define LOADUI_H //include LoadUi.hpp</p>
<p dir="auto">#include &lt;QWidget&gt;<br />
#include &lt;QUiLoader&gt;<br />
#include &lt;QFile&gt;<br />
#include &lt;QString&gt;<br />
#include &lt;memory&gt;</p>
<p dir="auto">using namespace std;</p>
<p dir="auto">class LoadUi : public QObject<br />
{<br />
public:<br />
explicit LoadUi();<br />
~LoadUi();<br />
QWidget <em>createForm(const QString</em>, QWidget* = 0);</p>
<p dir="auto">private:<br />
unique_ptr&lt;QUiLoader&gt; loader;<br />
unique_ptr&lt;QFile&gt; ui_file;<br />
};</p>
<p dir="auto">#endif@</p>
<p dir="auto">@/<em>Class load .ui files cpp</em>/</p>
<p dir="auto">#include "LoadUi.hpp"</p>
<p dir="auto">LoadUi::LoadUi()<br />
{<br />
}</p>
<p dir="auto">LoadUi::~LoadUi()<br />
{<br />
}</p>
<p dir="auto">QWidget *LoadUi::createForm(const QString *url, QWidget *parent)<br />
{<br />
ui_file  (new QFile("Forms/MainWindow.ui"));<br />
loader ( new QUiLoader());<br />
ui_file-&gt;open(QIODevice::ReadOnly);</p>
<p dir="auto">return loader-&gt;load(ui_file, parent);<br />
}</p>
<p dir="auto">@</p>
]]></description><link>https://forum.qt.io/topic/41870/error-with-unique_ptr</link><generator>RSS for Node</generator><lastBuildDate>Mon, 18 May 2026 15:15:24 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/41870.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 27 May 2014 00:19:51 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Error with unique_ptr on Fri, 30 May 2014 23:53:21 GMT]]></title><description><![CDATA[<p dir="auto">[quote author="Exotic_Devel" date="1401309899"]The theory of using heap says, "Large objects and long lasting." But how to judge it?[/quote]That theory is good.</p>
<p dir="auto">However, you need to know this: <em>Most Qt classes already store their internal data on the heap</em>.</p>
<p dir="auto">Example:<br />
@<br />
QString longString = largeTextFile.readAll();<br />
@</p>
<p dir="auto">Let's say that <em>longString</em> contains 2 MB of text. The <em>longString</em> object itself is stored on the stack, but it only takes <em>a few bytes of stack space</em>. That's because the text is stored in the heap, which takes <em>2 MB of heap space</em>. The stack holds a <em>pointer</em> to the heap data.</p>
<p dir="auto">So, you can treat Qt objects as "small objects".</p>
<p dir="auto">[quote author="Chris Kawa" date="1401491792"]Since Qt uses implicit sharing[/quote]Here is an article about "implicit sharing":<a href="http://qt-project.org/doc/qt-5/implicit-sharing.html" target="_blank" rel="noopener noreferrer nofollow ugc">http://qt-project.org/doc/qt-5/implicit-sharing.html</a></p>
]]></description><link>https://forum.qt.io/post/230535</link><guid isPermaLink="true">https://forum.qt.io/post/230535</guid><dc:creator><![CDATA[JKSH]]></dc:creator><pubDate>Fri, 30 May 2014 23:53:21 GMT</pubDate></item><item><title><![CDATA[Reply to Error with unique_ptr on Fri, 30 May 2014 23:16:32 GMT]]></title><description><![CDATA[<p dir="auto">Since Qt uses implicit sharing and pimpl, the rule of thumb is (with very few exceptions): consider Qt objects small and cheap to pass around and copy(where copyable).</p>
<p dir="auto">As for what is short and long lived - if it goes "out" from scope it was created in it's long lived and should be managed manually (either smart pointers or naked new/delete). If you don't pass it or only pass it "in" eg. as function params in the same scope then it's considered to be short-lived(or local) and you should use RAII and automatic variables.</p>
<p dir="auto">Note that it gets complicated with multithreading but that's another story.</p>
]]></description><link>https://forum.qt.io/post/230530</link><guid isPermaLink="true">https://forum.qt.io/post/230530</guid><dc:creator><![CDATA[Chris Kawa]]></dc:creator><pubDate>Fri, 30 May 2014 23:16:32 GMT</pubDate></item><item><title><![CDATA[Reply to Error with unique_ptr on Wed, 28 May 2014 21:53:55 GMT]]></title><description><![CDATA[<p dir="auto">In you example,  both QUiLoader and QFile are short lived.</p>
<p dir="auto">Then it depends on your objects, whether they are big, use lots of memories etc...</p>
]]></description><link>https://forum.qt.io/post/230278</link><guid isPermaLink="true">https://forum.qt.io/post/230278</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Wed, 28 May 2014 21:53:55 GMT</pubDate></item><item><title><![CDATA[Reply to Error with unique_ptr on Wed, 28 May 2014 20:44:59 GMT]]></title><description><![CDATA[<p dir="auto">I still make confusion concerning the use of stack and heap. Usually I leave the stack primitive types, and put each and every object in the heap. In the style of JAVA.<br />
The theory of using heap says, "Large objects and long lasting." But how to judge it?</p>
]]></description><link>https://forum.qt.io/post/230271</link><guid isPermaLink="true">https://forum.qt.io/post/230271</guid><dc:creator><![CDATA[Exotic_Devel]]></dc:creator><pubDate>Wed, 28 May 2014 20:44:59 GMT</pubDate></item><item><title><![CDATA[Reply to Error with unique_ptr on Wed, 28 May 2014 20:16:04 GMT]]></title><description><![CDATA[<p dir="auto">Yes they are and it's completely fine, why ?</p>
]]></description><link>https://forum.qt.io/post/230266</link><guid isPermaLink="true">https://forum.qt.io/post/230266</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Wed, 28 May 2014 20:16:04 GMT</pubDate></item><item><title><![CDATA[Reply to Error with unique_ptr on Wed, 28 May 2014 12:48:30 GMT]]></title><description><![CDATA[<p dir="auto">understand, correct me if I'm wrong.</p>
<p dir="auto">In your example, QUiLoad and QFile are being placed on the stack.</p>
]]></description><link>https://forum.qt.io/post/230219</link><guid isPermaLink="true">https://forum.qt.io/post/230219</guid><dc:creator><![CDATA[Exotic_Devel]]></dc:creator><pubDate>Wed, 28 May 2014 12:48:30 GMT</pubDate></item><item><title><![CDATA[Reply to Error with unique_ptr on Tue, 27 May 2014 16:14:11 GMT]]></title><description><![CDATA[<p dir="auto">I didn't say to always allocate on the stack far from it. I'm just talking about your usage of QString, have a look at the code examples of Qt and the how it is used.</p>
]]></description><link>https://forum.qt.io/post/230040</link><guid isPermaLink="true">https://forum.qt.io/post/230040</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Tue, 27 May 2014 16:14:11 GMT</pubDate></item><item><title><![CDATA[Reply to Error with unique_ptr on Tue, 27 May 2014 15:59:38 GMT]]></title><description><![CDATA[<p dir="auto">Always allocate on the stack?<br />
Someone told me that is not a good practice to place objects on the stack.</p>
]]></description><link>https://forum.qt.io/post/230034</link><guid isPermaLink="true">https://forum.qt.io/post/230034</guid><dc:creator><![CDATA[Exotic_Devel]]></dc:creator><pubDate>Tue, 27 May 2014 15:59:38 GMT</pubDate></item><item><title><![CDATA[Reply to Error with unique_ptr on Tue, 27 May 2014 12:53:50 GMT]]></title><description><![CDATA[<p dir="auto">Like in the example:</p>
<p dir="auto">@QWidget *LoadUi::createForm(const QString url, QWidget *parent)<br />
{<br />
QUiLoader loader;<br />
QFile file(url);<br />
file.open(QFile::ReadOnly); // add check here unless you did before<br />
QWidget *widget = loader.load(&amp;file, parent);<br />
file.close();<br />
return widget;<br />
}@</p>
<p dir="auto">Also, why are you using const QString * ? Just use a const reference, theres no need to allocate QString on the heap.</p>
]]></description><link>https://forum.qt.io/post/230011</link><guid isPermaLink="true">https://forum.qt.io/post/230011</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Tue, 27 May 2014 12:53:50 GMT</pubDate></item><item><title><![CDATA[Reply to Error with unique_ptr on Tue, 27 May 2014 12:37:46 GMT]]></title><description><![CDATA[<p dir="auto">The intention is to avoid manual delete. How would you do in this case?</p>
]]></description><link>https://forum.qt.io/post/230010</link><guid isPermaLink="true">https://forum.qt.io/post/230010</guid><dc:creator><![CDATA[Exotic_Devel]]></dc:creator><pubDate>Tue, 27 May 2014 12:37:46 GMT</pubDate></item><item><title><![CDATA[Reply to Error with unique_ptr on Tue, 27 May 2014 07:22:25 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">Can you explain why do you want to use unique_ptr in that case ? It's no really useful</p>
]]></description><link>https://forum.qt.io/post/229979</link><guid isPermaLink="true">https://forum.qt.io/post/229979</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Tue, 27 May 2014 07:22:25 GMT</pubDate></item><item><title><![CDATA[Reply to Error with unique_ptr on Tue, 27 May 2014 00:58:51 GMT]]></title><description><![CDATA[<p dir="auto">I found the error. the definition of the pointers were arrested in CreateForm method. I moved from global to local.</p>
<p dir="auto">@/<em>Class load .ui files cpp</em>/</p>
<p dir="auto">#include "LoadUi.hpp"</p>
<p dir="auto">LoadUi::LoadUi()<br />
{<br />
}</p>
<p dir="auto">LoadUi::~LoadUi()<br />
{<br />
}</p>
<p dir="auto">QWidget *LoadUi::createForm(const QString *url, QWidget *parent)<br />
{<br />
unique_ptr&lt;QUiLoader&gt; loader ( new QUiLoader());<br />
unique_ptr&lt;QFile&gt; ui_file (new QFile("Forms/MainWindow.ui"));</p>
<pre><code>return loader-&gt;load(ui_file, parent);
</code></pre>
<p dir="auto">}</p>
<p dir="auto">@</p>
]]></description><link>https://forum.qt.io/post/229949</link><guid isPermaLink="true">https://forum.qt.io/post/229949</guid><dc:creator><![CDATA[Exotic_Devel]]></dc:creator><pubDate>Tue, 27 May 2014 00:58:51 GMT</pubDate></item></channel></rss>