<?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[Need a few secrets about Qt in C&#x2F;C++...]]></title><description><![CDATA[<p dir="auto">I've been using C/C++ for nearly 30 years.  I've been using Qt for nearly 2 weeks.  One thing that mystifies me a bit is how widgets and and layouts are created (programmatically); much of the examples show Qt making, say, a form, and creating, say, a QLineEdit on the stack in the procedure, attaching it to the layout, and then return from the procedure.  I would be expecting this to throw the QLineEdit out of scope, and have it crash, but Qt seems quite happy.  Does it copy it over, instead of referencing it?</p>
<p dir="auto">Rough pseudo-code example:</p>
<p dir="auto">void addElementsToForm()<br />
{<br />
QFormLayout *thisForm =new QFormLayout(mainLayout);<br />
thisForm-&gt;addRow(new QLineEdit(_tr("status"));<br />
}</p>
<p dir="auto">I would think both the 'thisForm' and the instance of QLineEdit would go out of scope, but it seems happy.  Can someone confirm this is correct?<br />
Also, if you create a new QLineEdit here that you need to access later (out of this procedure), how do you get access to that?  Do you crawl the children looking for the name you gave the label ("status") or do a find child?  My inclination is to save that instance in a class object so I can get at it, but it seems a lot of examples don't bother with that.  Any insight is appreciated.  Thanks</p>
]]></description><link>https://forum.qt.io/topic/53600/need-a-few-secrets-about-qt-in-c-c</link><generator>RSS for Node</generator><lastBuildDate>Wed, 06 May 2026 08:41:02 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/53600.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 24 Apr 2015 15:50:50 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Need a few secrets about Qt in C&#x2F;C++... on Fri, 24 Apr 2015 19:09:24 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/tabj">@<bdi>Tabj</bdi></a> You might benefit from <a href="http://bit.ly/qtTrilogy" target="_blank" rel="noopener noreferrer nofollow ugc">my three courses on Qt in the Pluralsight library</a>. PS is currently having a 40% off sale (until April 30th) on annual subscriptions. If you want a week long VIP pass (should be long enough to watch the three Qt courses) send me a chat through this forum and I'll get you one.  You might also enjoy a lot of the C++ offerings especially if you're not up to speed on C++x11.</p>
]]></description><link>https://forum.qt.io/post/271404</link><guid isPermaLink="true">https://forum.qt.io/post/271404</guid><dc:creator><![CDATA[Rolias]]></dc:creator><pubDate>Fri, 24 Apr 2015 19:09:24 GMT</pubDate></item><item><title><![CDATA[Reply to Need a few secrets about Qt in C&#x2F;C++... on Fri, 24 Apr 2015 17:35:51 GMT]]></title><description><![CDATA[<p dir="auto">You're right but sometimes you don't need to store a pointer to these objects because you handle the value in signals&amp;slots</p>
<p dir="auto">For instance:</p>
<pre><code>class MyObject: public QWidget 
{
  Q_OBJECT

...
Q_SIGNALS:
    void newText(const QString&amp; text);

public Q_SLOTS:
    void handleText(const QString&amp;);

...
};
</code></pre>
<pre><code>MyObject::MyObject(QWidget* parent): QWidget(parent)
{
...
    QLineEdit *le = new QLineEdit(this);
    connect (le, &amp;QLineEdit::textChanged, this, &amp;MyObject::handleText);
    connect (this, &amp;MyObject::newText, le, &amp;QLineEdit::setText);
...
}
</code></pre>
]]></description><link>https://forum.qt.io/post/271388</link><guid isPermaLink="true">https://forum.qt.io/post/271388</guid><dc:creator><![CDATA[mcosta]]></dc:creator><pubDate>Fri, 24 Apr 2015 17:35:51 GMT</pubDate></item><item><title><![CDATA[Reply to Need a few secrets about Qt in C&#x2F;C++... on Fri, 24 Apr 2015 17:26:10 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mcosta">@<bdi>mcosta</bdi></a> Yes, of course, but examples have lots of line edits created on the fly but no way to save the pointer.  A use-once-and-discard, like a label, I don't mind ignoring (assuming you don't have to change it) but some of these other widgets you obviously have to access later.  Thanks</p>
]]></description><link>https://forum.qt.io/post/271384</link><guid isPermaLink="true">https://forum.qt.io/post/271384</guid><dc:creator><![CDATA[Tabj]]></dc:creator><pubDate>Fri, 24 Apr 2015 17:26:10 GMT</pubDate></item><item><title><![CDATA[Reply to Need a few secrets about Qt in C&#x2F;C++... on Fri, 24 Apr 2015 17:17:45 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/tabj">@<bdi>Tabj</bdi></a> said:</p>
<blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jksh">@<bdi>JKSH</bdi></a> says he keeps pointers around, which is my inclination too</p>
</blockquote>
<p dir="auto">Honestly I keep pointers only to objects that I need to manage directly after creation (for example I don't keep pointers to QLabel if I don't need to change their text; the same for layouts and so on..)</p>
]]></description><link>https://forum.qt.io/post/271383</link><guid isPermaLink="true">https://forum.qt.io/post/271383</guid><dc:creator><![CDATA[mcosta]]></dc:creator><pubDate>Fri, 24 Apr 2015 17:17:45 GMT</pubDate></item><item><title><![CDATA[Reply to Need a few secrets about Qt in C&#x2F;C++... on Fri, 24 Apr 2015 17:14:06 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jksh">@<bdi>JKSH</bdi></a> Thanks, makes sense - the examples were confusing me - wouldn't you want access to one of those fields after?  That's why I couldn't figure out why they seemed to be skirting that issue.  If it's for the sake of brevity in examples, that's understandable (though I'd like some note somewhere on it); my inclination is to hold onto a pointer too, so that's what I'll go ahead with.  Just wanted to make sure I wasn't missing something here.  Thanks.</p>
]]></description><link>https://forum.qt.io/post/271381</link><guid isPermaLink="true">https://forum.qt.io/post/271381</guid><dc:creator><![CDATA[Tabj]]></dc:creator><pubDate>Fri, 24 Apr 2015 17:14:06 GMT</pubDate></item><item><title><![CDATA[Reply to Need a few secrets about Qt in C&#x2F;C++... on Fri, 24 Apr 2015 17:08:41 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mcosta">@<bdi>mcosta</bdi></a> Hi and thanks; it wasn't so much the object being on the heap but the pointer was local to procedure and would go out of scope, so I didn't know how people were handling it.  <a class="plugin-mentions-user plugin-mentions-a" href="/user/jksh">@<bdi>JKSH</bdi></a> says he keeps pointers around, which is my inclination too, so maybe it's just the examples cheaping it out.  Thanks!</p>
]]></description><link>https://forum.qt.io/post/271380</link><guid isPermaLink="true">https://forum.qt.io/post/271380</guid><dc:creator><![CDATA[Tabj]]></dc:creator><pubDate>Fri, 24 Apr 2015 17:08:41 GMT</pubDate></item><item><title><![CDATA[Reply to Need a few secrets about Qt in C&#x2F;C++... on Fri, 24 Apr 2015 16:21:54 GMT]]></title><description><![CDATA[<p dir="auto">Hi, and welcome to the Qt Dev Net!</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/tabj">@<bdi>Tabj</bdi></a> said:</p>
<blockquote>
<p dir="auto">void addElementsToForm()<br />
{<br />
QFormLayout *thisForm =new QFormLayout(mainLayout);<br />
thisForm-&gt;addRow(new QLineEdit(_tr("status"));<br />
}</p>
<p dir="auto">I would think both the 'thisForm' and the instance of QLineEdit would go out of scope</p>
</blockquote>
<p dir="auto">Both the QFormLayout and the QLineEdit are created using <code>new</code>, so they are put in the heap, not the stack.</p>
<blockquote>
<p dir="auto">Also, if you create a new QLineEdit here that you need to access later (out of this procedure), how do you get access to that? Do you crawl the children looking for the name you gave the label ("status") or do a find child? My inclination is to save that instance in a class object so I can get at it, but it seems a lot of examples don't bother with that.</p>
</blockquote>
<p dir="auto">Yes, you can save a copy of the pointer. The examples didn't bother, probably because they didn't need access to the pointer afterwards.</p>
<p dir="auto">Crawling the child list works too, but it's probably a bit slower.</p>
<p dir="auto">Like you, I'm inclined to save the pointer.</p>
]]></description><link>https://forum.qt.io/post/271368</link><guid isPermaLink="true">https://forum.qt.io/post/271368</guid><dc:creator><![CDATA[JKSH]]></dc:creator><pubDate>Fri, 24 Apr 2015 16:21:54 GMT</pubDate></item><item><title><![CDATA[Reply to Need a few secrets about Qt in C&#x2F;C++... on Fri, 24 Apr 2015 16:21:03 GMT]]></title><description><![CDATA[<p dir="auto">Hi and welcome to devnet,</p>
<p dir="auto">little fix: <code>QFormLayout::addRow()</code> requires 2 parameters.</p>
<p dir="auto">BTW, Qt manages children in a very <em>user-friendly</em> way.<br />
Each time you create an <code>QObject</code> instance (or a subclass) you can specify the <code>parent</code>; each <code>QObject</code> has a list of his children (pointers) and handle the destruction of these automatically.<br />
For that reason is really suggested to create objects in the heap assigning their a paren</p>
<pre><code>void MyWidget::foo() {
    QWidget *w = new QWidget(this);
    QLabel* l = new QLabel(tr("Hello"), this);
}
</code></pre>
<p dir="auto">in that case <code>w</code> and <code>l</code> are <em>children</em> of <code>this</code>. <code>this</code> stores a pointers to <code>w</code> and <code>l</code> and when is destroyed it takes care of destroying all his children</p>
]]></description><link>https://forum.qt.io/post/271367</link><guid isPermaLink="true">https://forum.qt.io/post/271367</guid><dc:creator><![CDATA[mcosta]]></dc:creator><pubDate>Fri, 24 Apr 2015 16:21:03 GMT</pubDate></item></channel></rss>