<?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[QHBoxLayout delete QPushButton]]></title><description><![CDATA[<p dir="auto">Delete QPushButton in QHBoxLayout fail.</p>
<pre><code>QHBoxLayout *hbox = new QHBoxLayout;

void windowAdded(WId wid){
    Dock *dock = new Dock;
    dock-&gt;wid = wid;
    QPushButton *pushButton = new QPushButton(icon, NULL);
    pushButton-&gt;setUserData(DOCK, dock);
    hbox-&gt;addWidget(pushButton);
}

void windowRemoved(WId wid){
    for (int i=0; i&lt;hbox-&gt;count();i++) {
        QLayoutItem *LI = hbox-&gt;layout()-&gt;itemAt((i));
        QPushButton *pushButton = qobject_cast&lt;QPushButton*&gt;(LI-&gt;widget());
        Dock *dock = (Dock*)(pushButton-&gt;userData(DOCK));
        if (dock-&gt;wid == wid) {
            delete LI;
        }
    }
}
</code></pre>
]]></description><link>https://forum.qt.io/topic/113029/qhboxlayout-delete-qpushbutton</link><generator>RSS for Node</generator><lastBuildDate>Fri, 17 Apr 2026 05:19:49 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/113029.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 28 Mar 2020 10:44:15 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QHBoxLayout delete QPushButton on Tue, 31 Mar 2020 05:20:06 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/simonschroeder">@<bdi>SimonSchroeder</bdi></a> Strange solved by these:<br />
A layout in a big layout. delete cause overlap.<br />
Change to:<br />
A QWidget with the layout in a big layout, delete work fine.</p>
<pre><code>QList&lt;QPushButton *&gt; list_pushButton = widget_app-&gt;findChildren&lt;QPushButton *&gt;();    //QWidget
for (int i=0; i&lt;list_pushButton.count(); i++) {
    Dock *dock = (Dock*)(list_pushButton.at(i)-&gt;userData(DOCK));
            if (dock-&gt;wid == wid) {
                hbox_app-&gt;removeWidget(list_pushButton.at(i));    // QHBoxLayout in QWidget
                list_pushButton.at(i)-&gt;deleteLater();
            }
}
</code></pre>
]]></description><link>https://forum.qt.io/post/585707</link><guid isPermaLink="true">https://forum.qt.io/post/585707</guid><dc:creator><![CDATA[sonichy]]></dc:creator><pubDate>Tue, 31 Mar 2020 05:20:06 GMT</pubDate></item><item><title><![CDATA[Reply to QHBoxLayout delete QPushButton on Mon, 30 Mar 2020 07:32:34 GMT]]></title><description><![CDATA[<p dir="auto">I have to disagree with mrjj. It is true that the layout owns the push button. However, IIRC the push button also knows its parent and will delete itself from it when deleted. So, at least in theory your code should work.</p>
<p dir="auto">Nevertheless, I am not sure if you correctly use <code>setUserData</code>/<code>userData</code>. I have never encountered it and the reason for this is that it is not part of the public API. So, the best advice is: Don't use it! Quick googling gave me this: <a href="https://stackoverflow.com/questions/8260763/about-using-an-undocumented-class-in-qt" target="_blank" rel="noopener noreferrer nofollow ugc">https://stackoverflow.com/questions/8260763/about-using-an-undocumented-class-in-qt</a> Their advice is to use <code>QObject::setProperty</code> instead. You are more likely to get help in this forum if you use only documented features.</p>
]]></description><link>https://forum.qt.io/post/585492</link><guid isPermaLink="true">https://forum.qt.io/post/585492</guid><dc:creator><![CDATA[SimonSchroeder]]></dc:creator><pubDate>Mon, 30 Mar 2020 07:32:34 GMT</pubDate></item><item><title><![CDATA[Reply to QHBoxLayout delete QPushButton on Sat, 28 Mar 2020 11:00:18 GMT]]></title><description><![CDATA[<p dir="auto">Hi<br />
The layout owns it so<br />
you must use<br />
<a href="https://doc.qt.io/qt-5/qlayout.html#takeAt" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-5/qlayout.html#takeAt</a></p>
<p dir="auto">and delete that. ( will also delete its widget as far as i remembers )</p>
]]></description><link>https://forum.qt.io/post/585245</link><guid isPermaLink="true">https://forum.qt.io/post/585245</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Sat, 28 Mar 2020 11:00:18 GMT</pubDate></item></channel></rss>