<?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[Overlaying text on top of QIcon in QListWidget in Pyside2]]></title><description><![CDATA[<p dir="auto">Say I have a QListWidget in icon mode with multiple QIcons within it that looks like this:</p>
<p dir="auto"><img src="https://imgur.com/fYcXai5.jpg" alt="" class=" img-fluid img-markdown" /></p>
<p dir="auto">I populated the QListwidget very simply by looping over the file paths for the images I want to add to the QListWidget:</p>
<pre><code>for path in pathlist:
    self.HomeListWidget.addItem(QtWidgets.QListWidgetItem(QtGui.QIcon(path),None))

</code></pre>
<p dir="auto">How can I overlay text on top of the QIcon without modifying the image? I want the final result to look much like this:</p>
<p dir="auto"><img src="https://imgur.com/mG3tKfb.jpg" alt="alt text" class=" img-fluid img-markdown" /></p>
]]></description><link>https://forum.qt.io/topic/96127/overlaying-text-on-top-of-qicon-in-qlistwidget-in-pyside2</link><generator>RSS for Node</generator><lastBuildDate>Fri, 06 Mar 2026 06:27:50 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/96127.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 31 Oct 2018 01:29:12 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Overlaying text on top of QIcon in QListWidget in Pyside2 on Thu, 08 Nov 2018 08:24:26 GMT]]></title><description><![CDATA[<blockquote>
<p dir="auto">Do I just call item.setData(QtCore.Qt.UserRole, "New text goes here") again?</p>
</blockquote>
<p dir="auto">Correct</p>
<blockquote>
<p dir="auto">What method can I call on a QListWidgetItem to easily disable the text?</p>
</blockquote>
<p dir="auto">before <code>text = index.data(QtCore.Qt.UserRole)</code> add</p>
<pre><code class="language-python">if(index.data(QtCore.Qt.UserRole+1)):
    return
</code></pre>
<p dir="auto">Now you can use <code>item.setData(QtCore.Qt.UserRole+1,true)</code> to disable the text and <code>item.setData(QtCore.Qt.UserRole+1,false)</code> to enable it again</p>
<blockquote>
<p dir="auto">How do I make the text overlaying the QIcon red(it defaults to black) and change the font size?</p>
</blockquote>
<p dir="auto">replace <code>style.drawItemText(painter, option.rect,QtCore.Qt.AlignCenter, option.palette, option.state &amp; QStyle.State_Enabled, text)</code> with (using, C++ here, sorry)</p>
<pre><code class="language-cpp">painter-&gt;save();
QPen pen = painter-&gt;pen();
pen.setColor(Qt::red); // color
painter-&gt;setPen(pen);
QFont fnt = painter-&gt;font();
fnt.setPointSize(12); // font size
painter-&gt;setFont(fnt);
style-&gt;drawItemText(painter, option.rect, Qt::AlignCenter, option.palette, option.state &amp; QStyle::State_Enabled, text.toString());
painter-&gt;restore();
</code></pre>
]]></description><link>https://forum.qt.io/post/491912</link><guid isPermaLink="true">https://forum.qt.io/post/491912</guid><dc:creator><![CDATA[VRonin]]></dc:creator><pubDate>Thu, 08 Nov 2018 08:24:26 GMT</pubDate></item><item><title><![CDATA[Reply to Overlaying text on top of QIcon in QListWidget in Pyside2 on Thu, 08 Nov 2018 05:33:08 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/raisinbread22">@<bdi>RaisinBread22</bdi></a></p>
<ol>
<li><a href="http://doc.qt.io/qt-5/qlistwidgetitem.html#setText" target="_blank" rel="noopener noreferrer nofollow ugc">http://doc.qt.io/qt-5/qlistwidgetitem.html#setText</a></li>
<li>What do you mean by "disable the text"?</li>
<li>Qt style sheets: <a href="http://doc.qt.io/qt-5/stylesheet-examples.html" target="_blank" rel="noopener noreferrer nofollow ugc">http://doc.qt.io/qt-5/stylesheet-examples.html</a></li>
</ol>
]]></description><link>https://forum.qt.io/post/491875</link><guid isPermaLink="true">https://forum.qt.io/post/491875</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Thu, 08 Nov 2018 05:33:08 GMT</pubDate></item><item><title><![CDATA[Reply to Overlaying text on top of QIcon in QListWidget in Pyside2 on Thu, 08 Nov 2018 03:33:43 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vronin">@<bdi>VRonin</bdi></a></p>
<p dir="auto">Terrific, after making minor corrections I got text to display in the center of the Qicon! All I have left to do are 3 things.</p>
<ol>
<li>What method can I call on a QListWidgetItem to easily change the text to something different? Do I just call <code>item.setData(QtCore.Qt.UserRole, "New text goes here")</code> again?</li>
<li>What method can I call on a QListWidgetItem to easily disable the text?</li>
<li>How do I make the text overlaying the QIcon red(it defaults to black) and change the font size?</li>
</ol>
<p dir="auto">Here's the corrected(working) subclass for reference:</p>
<pre><code>class TextOverDelegate(QStyledItemDelegate):
    def __init__(self, parent=None):
        super(TextOverDelegate, self).__init__(parent)

    def paint(self, painter, option, index):
        super(TextOverDelegate,self).paint(painter,option,index)
        text = index.data(QtCore.Qt.UserRole)
        if(option.widget):
            style = option.widget.style()
        else:
            style = QApplication.style()
        style.drawItemText(painter, option.rect,QtCore.Qt.AlignCenter, option.palette, option.state &amp; QStyle.State_Enabled, text)
</code></pre>
<p dir="auto">Here's what it currently produces after  creating the QListWidgetItem with <code>item = QtWidgets.QListWidgetItem(QtGui.QIcon(path), None)</code> and calling <code>item.setData(QtCore.Qt.UserRole, "Test Qicon text")</code> before finally calling <code>self.listWidget.addItem(item)</code></p>
<p dir="auto"><img src="https://i.gyazo.com/151413c7dada4cde477073694e3ae696.png" alt="alt text" class=" img-fluid img-markdown" /></p>
]]></description><link>https://forum.qt.io/post/491868</link><guid isPermaLink="true">https://forum.qt.io/post/491868</guid><dc:creator><![CDATA[RaisinBread22]]></dc:creator><pubDate>Thu, 08 Nov 2018 03:33:43 GMT</pubDate></item><item><title><![CDATA[Reply to Overlaying text on top of QIcon in QListWidget in Pyside2 on Wed, 07 Nov 2018 08:45:39 GMT]]></title><description><![CDATA[<p dir="auto">On top of the above, you forgot to translate the first line: <code>QStyledItemDelegate::paint(painter,option,index);</code>. should be something like <code>super(TextOverDelegate,self,painter,option,index).paint(painter,option,index)</code></p>
]]></description><link>https://forum.qt.io/post/491715</link><guid isPermaLink="true">https://forum.qt.io/post/491715</guid><dc:creator><![CDATA[VRonin]]></dc:creator><pubDate>Wed, 07 Nov 2018 08:45:39 GMT</pubDate></item><item><title><![CDATA[Reply to Overlaying text on top of QIcon in QListWidget in Pyside2 on Wed, 07 Nov 2018 07:11:12 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jsulm">@<bdi>jsulm</bdi></a><br />
Hi<br />
Maybe he meant</p>
<p dir="auto">if(option.widget):<br />
style = option.widget.style()<br />
else:<br />
style = QApplication.style()</p>
<p dir="auto">aka use widget style / use global style</p>
]]></description><link>https://forum.qt.io/post/491697</link><guid isPermaLink="true">https://forum.qt.io/post/491697</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Wed, 07 Nov 2018 07:11:12 GMT</pubDate></item><item><title><![CDATA[Reply to Overlaying text on top of QIcon in QListWidget in Pyside2 on Wed, 07 Nov 2018 05:36:40 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/raisinbread22">@<bdi>RaisinBread22</bdi></a> said in <a href="/post/491682">Overlaying text on top of QIcon in QListWidget in Pyside2</a>:</p>
<blockquote>
<p dir="auto">style = option.widget</p>
</blockquote>
<p dir="auto">why do you assign widget to style? style has to be an instance of QStyle.</p>
]]></description><link>https://forum.qt.io/post/491685</link><guid isPermaLink="true">https://forum.qt.io/post/491685</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Wed, 07 Nov 2018 05:36:40 GMT</pubDate></item><item><title><![CDATA[Reply to Overlaying text on top of QIcon in QListWidget in Pyside2 on Thu, 08 Nov 2018 01:39:40 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vronin">@<bdi>VRonin</bdi></a></p>
<p dir="auto">Thanks a bunch for the detailed response! I tried implementing the sublclass of QStyledItemDelegate in Pyside2, however I'm getting <code>AttributeError: 'PySide2.QtWidgets.QListWidget' object has no attribute 'drawItemText'</code> I understand you are not super familiar with Python, but might you or somebody else who is be able to identify what I did wrong? Thank you!</p>
<p dir="auto"><strong>My Pyside 2 implementation:</strong></p>
<pre><code>class TextOverDelegate(QStyledItemDelegate):
    def __init__(self, parent=None):
        super(TextOverDelegate, self).__init__(parent)

    def paint(self, painter, option, index):
        text = index.data(QtCore.Qt.UserRole)
        if(option.widget):
            style = option.widget
        else:
            style = QApplication.style()
        style.drawItemText(painter, option.rect,QtCore.Qt.AlignCenter, option.palette, option.state &amp; QStyle.State_Enabled, text)
</code></pre>
<p dir="auto">In the main UI window class all I set the new delegate as the list widget's current item delegate.</p>
<pre><code>self.custom_delegate = TextOverDelegate()
self.listWidget.setItemDelegate(self.custom_delegate)
</code></pre>
]]></description><link>https://forum.qt.io/post/491682</link><guid isPermaLink="true">https://forum.qt.io/post/491682</guid><dc:creator><![CDATA[RaisinBread22]]></dc:creator><pubDate>Thu, 08 Nov 2018 01:39:40 GMT</pubDate></item><item><title><![CDATA[Reply to Overlaying text on top of QIcon in QListWidget in Pyside2 on Thu, 01 Nov 2018 09:24:03 GMT]]></title><description><![CDATA[<p dir="auto">Sorry but I'll use C++ as I don't know python</p>
<pre><code class="language-cpp">class TextOverDelegate : public QStyledItemDelegate{
    Q_OBJECT
    Q_DISABLE_COPY(TextOverDelegate)
public:
    explicit TextOverDelegate(QObject* parent = Q_NULLPTR)
        :QStyledItemDelegate(parent)
    {}
    void paint(QPainter *painter, const QStyleOptionViewItem &amp;option, const QModelIndex &amp;index) const Q_DECL_OVERRIDE{
        QStyledItemDelegate::paint(painter,option,index);
        const QVariant text = index.data(Qt::UserRole);
        if(text.isValid()){
            const QStyle *style = option.widget ? option.widget-&gt;style() : QApplication::style();
            style-&gt;drawItemText(painter, option.rect, Qt::AlignCenter, option.palette, option.state &amp; QStyle::State_Enabled, text.toString());
        }
    }
};
</code></pre>
<p dir="auto">This delegate will paint the text stored using <code>QListWidgetItem::setData</code> passing <code>Qt::UserRole</code></p>
]]></description><link>https://forum.qt.io/post/490514</link><guid isPermaLink="true">https://forum.qt.io/post/490514</guid><dc:creator><![CDATA[VRonin]]></dc:creator><pubDate>Thu, 01 Nov 2018 09:24:03 GMT</pubDate></item><item><title><![CDATA[Reply to Overlaying text on top of QIcon in QListWidget in Pyside2 on Wed, 31 Oct 2018 20:14:52 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">There's no need at all for setItemWidget nor a custom widget from a QItemDelegate. A subclass of <a href="http://doc.qt.io/qt-5/qstyleditemdelegate.html" target="_blank" rel="noopener noreferrer nofollow ugc">QStyledItemDelegate</a> with its paint event re-implemented is enough.</p>
]]></description><link>https://forum.qt.io/post/490438</link><guid isPermaLink="true">https://forum.qt.io/post/490438</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Wed, 31 Oct 2018 20:14:52 GMT</pubDate></item><item><title><![CDATA[Reply to Overlaying text on top of QIcon in QListWidget in Pyside2 on Wed, 31 Oct 2018 19:03:52 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/dheerendra">@<bdi>dheerendra</bdi></a> said in <a href="/post/490393">Overlaying text on top of QIcon in QListWidget in Pyside2</a>:</p>
<blockquote>
<p dir="auto">Just your custom widget and set the custom Widget using setItemWidget(...) .</p>
<p dir="auto">2nd Option seems to easy one.</p>
</blockquote>
<p dir="auto">Where's @VRonin...?</p>
]]></description><link>https://forum.qt.io/post/490417</link><guid isPermaLink="true">https://forum.qt.io/post/490417</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Wed, 31 Oct 2018 19:03:52 GMT</pubDate></item><item><title><![CDATA[Reply to Overlaying text on top of QIcon in QListWidget in Pyside2 on Wed, 31 Oct 2018 16:39:15 GMT]]></title><description><![CDATA[<p dir="auto">Either you write your own delegate inherit from QItemDelegate &amp; return custom QWidget.</p>
<p dir="auto">OR<br />
Just your custom widget and set the custom Widget using setItemWidget(...) .</p>
<p dir="auto">2nd Option seems to easy one.</p>
]]></description><link>https://forum.qt.io/post/490393</link><guid isPermaLink="true">https://forum.qt.io/post/490393</guid><dc:creator><![CDATA[dheerendra]]></dc:creator><pubDate>Wed, 31 Oct 2018 16:39:15 GMT</pubDate></item><item><title><![CDATA[Reply to Overlaying text on top of QIcon in QListWidget in Pyside2 on Wed, 31 Oct 2018 15:12:15 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/dheerendra">@<bdi>dheerendra</bdi></a>  Thanks for your response. Do you have a link to a simple, complete example of somebody doing this? I haven't written my own delegate before, and all of the examples I've been able to find over the past few hours seem unnecessarily complicated for what I'm doing and It's very overwhelming. All the examples I've seen are inheriting from QItemDelegate, not QWidget. Cheers!</p>
]]></description><link>https://forum.qt.io/post/490372</link><guid isPermaLink="true">https://forum.qt.io/post/490372</guid><dc:creator><![CDATA[RaisinBread22]]></dc:creator><pubDate>Wed, 31 Oct 2018 15:12:15 GMT</pubDate></item><item><title><![CDATA[Reply to Overlaying text on top of QIcon in QListWidget in Pyside2 on Wed, 31 Oct 2018 04:26:35 GMT]]></title><description><![CDATA[<p dir="auto">You need to write your own delegate which is inherited from QWidget. Re-implement the paintEvent(..) function to achieve this.</p>
<p dir="auto">void paintEvent(....) {<br />
QPainter p(this).<br />
p.drawPixmap(icon)<br />
p.drawRect(..)<br />
}</p>
<p dir="auto">Set this widget object using setItemWidget(...) function.</p>
]]></description><link>https://forum.qt.io/post/490205</link><guid isPermaLink="true">https://forum.qt.io/post/490205</guid><dc:creator><![CDATA[dheerendra]]></dc:creator><pubDate>Wed, 31 Oct 2018 04:26:35 GMT</pubDate></item></channel></rss>