<?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[When using a custom delegate to paint the items of a QTableWidget, how can the delegate be configured to use the correct background colour for selected items?]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">Normally, when a QTableWidget item is selected, the background appears dark blue, but when I use a custom delegate to paint the items (taken from a Stack Overflow post, <a href="https://stackoverflow.com/questions/1956542/how-to-make-item-view-render-rich-html-text-in-qt/1956781#1956781" target="_blank" rel="noopener noreferrer nofollow ugc">https://stackoverflow.com/questions/1956542/how-to-make-item-view-render-rich-html-text-in-qt/1956781#1956781</a>) the background appears light blue instead.</p>
<p dir="auto">What do I need to do to match the original behaviour when using the delegate?</p>
<p dir="auto">Thanks.</p>
]]></description><link>https://forum.qt.io/topic/104677/when-using-a-custom-delegate-to-paint-the-items-of-a-qtablewidget-how-can-the-delegate-be-configured-to-use-the-correct-background-colour-for-selected-items</link><generator>RSS for Node</generator><lastBuildDate>Wed, 15 Jul 2026 10:25:14 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/104677.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 06 Jul 2019 09:47:58 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to When using a custom delegate to paint the items of a QTableWidget, how can the delegate be configured to use the correct background colour for selected items? on Mon, 15 Jul 2019 13:42:36 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mrjj">@<bdi>mrjj</bdi></a> Thanks! That's fixed it.</p>
]]></description><link>https://forum.qt.io/post/540865</link><guid isPermaLink="true">https://forum.qt.io/post/540865</guid><dc:creator><![CDATA[Bastian]]></dc:creator><pubDate>Mon, 15 Jul 2019 13:42:36 GMT</pubDate></item><item><title><![CDATA[Reply to When using a custom delegate to paint the items of a QTableWidget, how can the delegate be configured to use the correct background colour for selected items? on Sat, 06 Jul 2019 10:18:46 GMT]]></title><description><![CDATA[<p dir="auto">Hi<br />
Add some extra code to the painting (set the brush)</p>
<pre><code>void HtmlDelegate::paint(QPainter *painter, const QStyleOptionViewItem &amp;option,
                         const QModelIndex &amp;index) const
{
    QStyleOptionViewItemV4 optionV4 = option;
    initStyleOption(&amp;optionV4, index);

    QStyle *style = optionV4.widget ? optionV4.widget-&gt;style() : QApplication::style();

    QTextDocument doc;
    doc.setHtml(optionV4.text);

    /// Painting item without text
    optionV4.text = QString();

    if (option.state &amp; QStyle::State_Selected) { //////////// new
        painter-&gt;setPen(Qt::white);
        painter-&gt;setBrush(option.palette.highlightedText());
    }

    style-&gt;drawControl(QStyle::CE_ItemViewItem, &amp;option, painter, option.widget); // slightly changed

    QAbstractTextDocumentLayout::PaintContext ctx;

    // Highlighting text if item is selected
    if (optionV4.state &amp; QStyle::State_Selected) {
        ctx.palette.setColor(QPalette::Text, optionV4.palette.color(QPalette::Active,
                                                                    QPalette::HighlightedText));
        ctx.palette.setColor(QPalette::Highlight, Qt::green);
        ctx.palette.setColor(QPalette::Base, Qt::green);
    }

    QRect textRect = style-&gt;subElementRect(QStyle::SE_ItemViewItemText, &amp;optionV4);
    painter-&gt;save();
    painter-&gt;translate(textRect.topLeft());
    painter-&gt;setClipRect(textRect.translated(-textRect.topLeft()));
    doc.documentLayout()-&gt;draw(painter, ctx);
    painter-&gt;restore();
}

</code></pre>
<p dir="auto"><img src="http://imagizer.imageshack.com/img921/5235/Qu8zNs.png" alt="alt text" class=" img-fluid img-markdown" /></p>
]]></description><link>https://forum.qt.io/post/539407</link><guid isPermaLink="true">https://forum.qt.io/post/539407</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Sat, 06 Jul 2019 10:18:46 GMT</pubDate></item></channel></rss>