<?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[HTML rendering in QStyledItemDelegate sub-class]]></title><description><![CDATA[<p dir="auto">I need html rendering in a custom item delegate. What i have already done</p>
<h1>Write it using QTextDocument, setHtml and QTextDocument::draw.</h1>
<h1>Split QTextDocument into blocks using QTextDocument::begin() and draw each block using QTextBlock::charFormat() to setup the QPainter.</h1>
<p dir="auto">Why the dont work:</p>
<h1>Solution 1, it works, but since there is no text-overflow css property, I can't use ellipsis for long lines.</h1>
<h1>Solution 2, it also works, and i can manually use QFontMetrics::elidedText() so this solution is better but, the QTextCharFormat has wrong font information "althought the foreground and background colors are correct", for instance I used the &lt;b&gt;&lt;/b&gt; tag, so it should be bold but it isn't .</h1>
<p dir="auto">Here is relevant code for the second solution, thank you for your help in advance.</p>
<p dir="auto">@static void<br />
textDocumentDrawContents(QPainter *painter, const QTextDocument *document,<br />
int width, bool selected, const QPalette &amp;palette)<br />
{<br />
QTextBlock                   it;<br />
QAbstractTextDocumentLayout *layout;</p>
<pre><code>if ((painter == NULL) || (document == NULL))
    return;

layout = document-&gt;documentLayout();
if (layout == NULL)
    return;

QVector&lt;QTextFormat&gt; formats(document-&gt;allFormats());

for (it = document-&gt;begin() ; it != document-&gt;end() ; it = it.next())
{
    int           index;
    const QRectF &amp;rectangle(layout-&gt;blockBoundingRect(it));

    index = it.charFormatIndex();
    if ((index &lt; 0) || (index &gt;= formats.count()))
        continue;

    QTextFormat textformat(formats.at(index));
    if (textformat.isCharFormat() == false)
        continue;

    QString         text;
    QTextCharFormat format(textformat.toCharFormat());
    const QBrush   &amp;foreground(format.foreground());
    const QFont    &amp;font(format.font());
    QFontMetrics    metrics(font);

    text = metrics.elidedText(it.text(), Qt::ElideRight, width - 8);

    painter-&gt;setFont(font);
    if (selected == true)
        painter-&gt;setPen(palette.color(QPalette::HighlightedText));
    else
        painter-&gt;setPen(foreground.color());

    painter-&gt;drawText(rectangle, text);
}
</code></pre>
<p dir="auto">}</p>
<p dir="auto">void<br />
renderHtml(QPainter *painter, const QStyleOptionViewItem &amp;option, QTextDocument *document)<br />
{<br />
const QRect  &amp;frame(option.rect);<br />
bool          selected;<br />
bool          active;</p>
<pre><code>if (painter == NULL)
    return;

selected  = ((option.state &amp; QStyle::State_Selected) == QStyle::State_Selected);
active    = ((option.state &amp; QStyle::State_Active) == QStyle::State_Active);
selected  = ((selected == true) &amp;&amp; (active == true));

painter-&gt;save();
painter-&gt;translate(frame.left(), frame.top()); 

textDocumentDrawContents(painter, document, frame.width(), selected, option.palette);

painter-&gt;restore();
</code></pre>
<p dir="auto">}@</p>
]]></description><link>https://forum.qt.io/topic/37954/html-rendering-in-qstyleditemdelegate-sub-class</link><generator>RSS for Node</generator><lastBuildDate>Wed, 12 Aug 2026 23:12:45 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/37954.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 18 Feb 2014 01:21:54 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to HTML rendering in QStyledItemDelegate sub-class on Tue, 18 Feb 2014 01:22:59 GMT]]></title><description><![CDATA[<p dir="auto">Oh by the way. I am aware of QWebView, but I want a QWebViewLESS solution.</p>
]]></description><link>https://forum.qt.io/post/215255</link><guid isPermaLink="true">https://forum.qt.io/post/215255</guid><dc:creator><![CDATA[iharob]]></dc:creator><pubDate>Tue, 18 Feb 2014 01:22:59 GMT</pubDate></item></channel></rss>