<?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[QListWidget: problem with &quot;itemWidget&quot;]]></title><description><![CDATA[<p dir="auto">I try to reach a widget in a QListWidget, but the method "itemWidget" returns nothing, whereas the method "item" works correctly.<br />
Extract of the code:</p>
<pre><code>QWidget *currentWidget = nullptr;
    QListWidgetItem *currentItem = nullptr;
    QFont fontWidget;
    int numItem;

    numItem = listWidgetMain-&gt;currentRow();

    if ((numItem &gt;= 0) &amp;&amp; (numItem &lt; listWidgetMain-&gt;count()))
    {
        currentItem = new QListWidgetItem();
        currentWidget = new QWidget();
        // The item is correctly selected
        currentItem = listWidgetMain-&gt;item(numItem);
        // The problem is here: the widget is not returned
        currentWidget = listWidgetMain-&gt;itemWidget(currentItem);
        fontWidget = currentWidget-&gt;font();
    }
</code></pre>
<p dir="auto">The "currentItem" is ok, whereas the "currentWidget" is empty after the call to "itemWidget", and I don't understand why...<br />
Could someone explain me:</p>
<ul>
<li>Why it does not work</li>
<li>How to get the Widget address</li>
</ul>
<p dir="auto">The complete code is:</p>
<ul>
<li>mainwindow.h</li>
</ul>
<pre><code>#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include &lt;QMainWindow&gt;
#include &lt;QListWidget&gt;
#include &lt;QVBoxLayout&gt;
#include &lt;QPushButton&gt;
#include &lt;QWidget&gt;

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

public slots:
    void Test();

private:
    QWidget *centralWidget;
    QListWidget *listWidgetMain;
    QVBoxLayout *vertLayoutMain;
    QPushButton *BT_test;
};
#endif // MAINWINDOW_H

</code></pre>
<ul>
<li>mainwindow.cpp:</li>
</ul>
<pre><code>#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    int numLine;

    // QListWidget object
    listWidgetMain = new QListWidget();
    for (numLine = 0; numLine &lt; 5; numLine++)
    {
        listWidgetMain-&gt;addItem("text_" + QString::number(numLine + 1));
    }

    // Test button
    BT_test = new QPushButton("Test");
    QObject::connect(BT_test, SIGNAL(clicked()), this, SLOT(Test()));

    // Layout with the list built
    vertLayoutMain = new QVBoxLayout();
    vertLayoutMain-&gt;addWidget(listWidgetMain);
    vertLayoutMain-&gt;addWidget(BT_test);

    // Main widget
    centralWidget = new QWidget();
    setCentralWidget(centralWidget);
    centralWidget-&gt;setLayout(vertLayoutMain);
}

MainWindow::~MainWindow()
{
}

void MainWindow::Test()
{
    QWidget *currentWidget = nullptr;
    QListWidgetItem *currentItem = nullptr;
    QFont fontWidget;
    int numItem;

    numItem = listWidgetMain-&gt;currentRow();

    if ((numItem &gt;= 0) &amp;&amp; (numItem &lt; listWidgetMain-&gt;count()))
    {
        currentItem = new QListWidgetItem();
        currentWidget = new QWidget();
        // The item is correctly selected
        currentItem = listWidgetMain-&gt;item(numItem);
        // The problem is here: the widget is not returned
        currentWidget = listWidgetMain-&gt;itemWidget(currentItem);
        fontWidget = currentWidget-&gt;font();
    }
}
</code></pre>
<p dir="auto">Thank you in advance for your help!<br />
Laurent</p>
]]></description><link>https://forum.qt.io/topic/123369/qlistwidget-problem-with-itemwidget</link><generator>RSS for Node</generator><lastBuildDate>Sat, 11 Apr 2026 02:46:15 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/123369.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 02 Feb 2021 14:21:36 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QListWidget: problem with &quot;itemWidget&quot; on Tue, 02 Feb 2021 14:47:50 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/combas">@<bdi>Combas</bdi></a><br />
Items are not widgets; they can be made to <em>hold</em> widgets (<code>setItemWidget()</code>), but don't do so unless you make them do so.  If you can, try to stay away from putting any widgets of your own into tables/lists, they are "expensive".  You don't need to unless it's some special case, normally just work with the items it naturally uses.</p>
]]></description><link>https://forum.qt.io/post/641944</link><guid isPermaLink="true">https://forum.qt.io/post/641944</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Tue, 02 Feb 2021 14:47:50 GMT</pubDate></item><item><title><![CDATA[Reply to QListWidget: problem with &quot;itemWidget&quot; on Tue, 02 Feb 2021 14:41:19 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> Thank you for your quick and clear answer.<br />
Indeed, I thought that the "addItem" method added a widget... That was my mistake!</p>
]]></description><link>https://forum.qt.io/post/641942</link><guid isPermaLink="true">https://forum.qt.io/post/641942</guid><dc:creator><![CDATA[Combas]]></dc:creator><pubDate>Tue, 02 Feb 2021 14:41:19 GMT</pubDate></item><item><title><![CDATA[Reply to QListWidget: problem with &quot;itemWidget&quot; on Tue, 02 Feb 2021 14:30:45 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/combas">@<bdi>Combas</bdi></a> said in <a href="/post/641939">QListWidget: problem with "itemWidget"</a>:</p>
<blockquote>
<pre><code>    // The problem is here: the widget is not returned
    currentWidget = listWidgetMain-&gt;itemWidget(currentItem);
</code></pre>
</blockquote>
<p dir="auto">What widget do you think should be returned?  You obviously do not have any widget there, else it would be!</p>
<pre><code>        currentItem = new QListWidgetItem();
        currentWidget = new QWidget();
</code></pre>
<p dir="auto">I don't know what you are (intending to) doing here?  You don't don't use these two <code>new</code>ed items, so they are just a memory leak.</p>
<p dir="auto">You must have something conceptually wrong.  If you never put your widget into the list you'll never get it back from it.  You have no <code>setItemWidget()</code> anywhere, so you're not.</p>
<p dir="auto">P.S.<br />
If you think</p>
<pre><code> listWidgetMain-&gt;addItem("text_" + QString::number(numLine + 1));
</code></pre>
<p dir="auto">adds a <em>widget</em> it does <em>not</em>.  It only adds an <em>item</em>.  And unless you have some use case, you should not be using <code>setItemWidget()</code> or accessing <code>itemWidget()</code> anyway.</p>
]]></description><link>https://forum.qt.io/post/641940</link><guid isPermaLink="true">https://forum.qt.io/post/641940</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Tue, 02 Feb 2021 14:30:45 GMT</pubDate></item></channel></rss>