<?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[Why doesn&#x27;t this widget initialization work?]]></title><description><![CDATA[<p dir="auto">I tried the <a href="http://doc.qt.io/qt-5/qtwidgets-tutorials-widgets-childwidget-example.html" target="_blank" rel="noopener noreferrer nofollow ugc">"Child Widgets"</a> tutorial, and I thought I would be able to create a child pushbutton by doing direct initialization rather than creating a pointer. In the code below, I commented out the pointer lines and replaced them with direct initialization lines. When I tried to build, however, on the initialization line I get the error "cannot access private member declared in class 'QPushButton'". Why is this error called when I try direct initialization? I'm pretty sure I'm following the right syntax. Is it just a Qt rule that I must use pointers to work with widgets?</p>
<pre><code>#include &lt;QtWidgets&gt;

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    QWidget window;
    window.resize(320, 340);
    window.show();
    window.setToolTip("This is the Top-level widget");
    window.setWindowTitle(QApplication::translate("toplevel", "Top-level widget"));

    /*QPushButton button = new QPushButton(QApplication::translate("pushbutton", "Test Push"), &amp;window);
    button -&gt; show();
    button -&gt; move(100, 100); */
    QPushButton button(QPushButton(QApplication::translate("pushbutton", "Test Push"), &amp;window)); //error is here
    button.show();
    button.move(100,100);


    return app.exec();
</code></pre>
<p dir="auto">}</p>
]]></description><link>https://forum.qt.io/topic/73281/why-doesn-t-this-widget-initialization-work</link><generator>RSS for Node</generator><lastBuildDate>Mon, 15 Jun 2026 03:56:11 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/73281.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 12 Nov 2016 19:28:53 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Why doesn&#x27;t this widget initialization work? on Sat, 12 Nov 2016 20:30:29 GMT]]></title><description><![CDATA[<p dir="auto">Oh, thanks. I didn't even notice I called QPushButton twice. That was never my intention. Anyways, thanks for the code check. The link is helpful too.</p>
]]></description><link>https://forum.qt.io/post/359227</link><guid isPermaLink="true">https://forum.qt.io/post/359227</guid><dc:creator><![CDATA[DragonautX]]></dc:creator><pubDate>Sat, 12 Nov 2016 20:30:29 GMT</pubDate></item><item><title><![CDATA[Reply to Why doesn&#x27;t this widget initialization work? on Sat, 12 Nov 2016 20:42:45 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/dragonautx">@<bdi>DragonautX</bdi></a> said in <a href="/topic/73281/why-doesn-t-this-widget-initialization-work/1">Why doesn't this widget initialization work?</a>:</p>
<blockquote>
<p dir="auto">Why is this error called when I try direct initialization?</p>
</blockquote>
<p dir="auto">This has nothing to do with the stack allocation. The problem is that you're invoking the copy constructor of the <code>QPushButton</code> class and as all <code>QObject</code> descendants the copy constructor is made private (copying is forbidden). <a href="http://doc.qt.io/qt-5/qobject.html#no-copy-constructor-or-assignment-operator" target="_blank" rel="noopener noreferrer nofollow ugc">Here's</a> the long story why.</p>
<p dir="auto">If you remove the (unnecessary) copying for your push button, e.g.:</p>
<pre><code>QPushButton button(QApplication::translate("pushbutton", "Test Push"), &amp;window);
</code></pre>
<p dir="auto">you should get a clean build.</p>
<blockquote>
<p dir="auto">Is it just a Qt rule that I must use pointers to work with widgets?</p>
</blockquote>
<p dir="auto">There's no such rule. Stack allocation is perfectly fine, and in my opinion a 'zillion times better than creating on the heap with <code>new</code>.</p>
<p dir="auto">Kind regards.</p>
]]></description><link>https://forum.qt.io/post/359218</link><guid isPermaLink="true">https://forum.qt.io/post/359218</guid><dc:creator><![CDATA[kshegunov]]></dc:creator><pubDate>Sat, 12 Nov 2016 20:42:45 GMT</pubDate></item></channel></rss>