<?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[Model View alter display text]]></title><description><![CDATA[<p dir="auto">I have a model (<code>QStandardItemModel</code>-derived) and a view (<code>QTableView</code>-derived).  I wish to alter the displayed text for <em>certain rows</em> which hold a numeric to show as percentage, e.g. <code>30</code> -&gt; <code>30%</code>.  (The principle here would apply to any other transformation, e.g. <code>$30</code> or <code>(30)</code> for accountancy negative numbers, and is not uncommon in tables displaying, say, numeric values)</p>
<p dir="auto">I <em>used to</em> accomplish this by overriding <code>data(const QModelIndex &amp;index, int role = Qt::DisplayRole) const</code> having it go essentially:</p>
<pre><code>if (role == Qt::DisplayRole)
    if (index.row() == percentRow)
    {
        int val = Base::data(index, role).toInt();
        return QString("%1%").arg(val);
    }
</code></pre>
<p dir="auto">i.e. making it return the <em>string</em> I wanted instead of the numeric.  This was easy and I could do it in 30 seconds.</p>
<p dir="auto">I was told in no uncertain terms by experts here that this was wicked, I must leave the data type returned as-was for all sorts of reasons, not convert to string here.  I should use <code>QStyledItemDelegate</code> for this.  Fair enough.</p>
<p dir="auto">I come to do that now and was <em>assuming</em> I could use <code>QString QStyledItemDelegate::displayText(const QVariant &amp;value, const QLocale &amp;locale) const</code>, which would not have been too bad.  <em>However</em> I now see that does not convey the <code>QModelIndex</code>, so I cannot select by row/column.</p>
<p dir="auto">Which means I seem to be left with overriding <code>void QStyledItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &amp;option, const QModelIndex &amp;index) const</code>, and doing quite a bit of work there (converting to string and dealing with painters).</p>
<p dir="auto">Is this <em>really</em> what I have to do to alter the string displayed for a number?  Seems crazy to me to be going down into painters :(  There isn't anything I can do via the <code>QStyleOptionViewItem &amp;option</code>, is there?  Nor can I get a <code>displayText()</code> override to know about the <code>QModelIndex</code>, can I?</p>
]]></description><link>https://forum.qt.io/topic/114683/model-view-alter-display-text</link><generator>RSS for Node</generator><lastBuildDate>Mon, 13 Apr 2026 09:27:52 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/114683.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 11 May 2020 10:18:23 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Model View alter display text on Tue, 12 May 2020 08:59:03 GMT]]></title><description><![CDATA[<p dir="auto"><code>initStyleOption</code> is also virtual so you can do the chage directly there with minimal effort.</p>
<pre><code class="language-cpp">class ListviewDelegate : public QStyledItemDelegate{
    Q_OBJECT
    Q_DISABLE_COPY_MOVE(ListviewDelegate)
public:
    explicit ListviewDelegate(QObject* parent = nullptr)
        : QStyledItemDelegate(parent)
    {}
protected:
    void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &amp;index) const override{
        QStyledItemDelegate::initStyleOption(option,index);
        if (index.row() == percentRow)
            option-&gt;text += QChar('%');
    }
};
</code></pre>
<p dir="auto">if you need to do anything more fancy to the text you can just use <code>option-&gt;text = doSomethingFancy(displayText(index.data(Qt::DisplayRole), option-&gt;locale));</code></p>
]]></description><link>https://forum.qt.io/post/594213</link><guid isPermaLink="true">https://forum.qt.io/post/594213</guid><dc:creator><![CDATA[VRonin]]></dc:creator><pubDate>Tue, 12 May 2020 08:59:03 GMT</pubDate></item><item><title><![CDATA[Reply to Model View alter display text on Tue, 12 May 2020 10:15:24 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vronin">@<bdi>VRonin</bdi></a><br />
Thank you, this looks much more like a reasonable approach!  No offence to <a class="plugin-mentions-user plugin-mentions-a" href="/user/mrjj">@<bdi>mrjj</bdi></a>'s :)  I will go investigate this now and report back....</p>
<p dir="auto">...and I am pleased to report this works as desired, and in a reasonable code fashion.  The secret was indeed that <code>initStyleOption()</code> is virtual.</p>
]]></description><link>https://forum.qt.io/post/594215</link><guid isPermaLink="true">https://forum.qt.io/post/594215</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Tue, 12 May 2020 10:15:24 GMT</pubDate></item><item><title><![CDATA[Reply to Model View alter display text on Tue, 12 May 2020 08:59:03 GMT]]></title><description><![CDATA[<p dir="auto"><code>initStyleOption</code> is also virtual so you can do the chage directly there with minimal effort.</p>
<pre><code class="language-cpp">class ListviewDelegate : public QStyledItemDelegate{
    Q_OBJECT
    Q_DISABLE_COPY_MOVE(ListviewDelegate)
public:
    explicit ListviewDelegate(QObject* parent = nullptr)
        : QStyledItemDelegate(parent)
    {}
protected:
    void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &amp;index) const override{
        QStyledItemDelegate::initStyleOption(option,index);
        if (index.row() == percentRow)
            option-&gt;text += QChar('%');
    }
};
</code></pre>
<p dir="auto">if you need to do anything more fancy to the text you can just use <code>option-&gt;text = doSomethingFancy(displayText(index.data(Qt::DisplayRole), option-&gt;locale));</code></p>
]]></description><link>https://forum.qt.io/post/594213</link><guid isPermaLink="true">https://forum.qt.io/post/594213</guid><dc:creator><![CDATA[VRonin]]></dc:creator><pubDate>Tue, 12 May 2020 08:59:03 GMT</pubDate></item><item><title><![CDATA[Reply to Model View alter display text on Mon, 11 May 2020 15:33:17 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mrjj">@<bdi>mrjj</bdi></a><br />
Thank you for supplying this.  Looks to me like some damn hairy code to append a <code>%</code> to a number :)</p>
<p dir="auto">I need to look through the docs for this tomorrow to understand what is going on.  Since I won't want to interfere with whatever <code>paint()</code> does or does not do <em>for all other cases</em>, I guess I'll want something like:</p>
<pre><code>if (index.row() == percentRow)
{
    QVariant v = data(index);
    if (v.somethingAboutTypeIsNumber())
    {
        ( your code, using  QString("%1%").arg(v);, but I don't think that accepts a QVariant )
        return;
    }
}
QStyledItemDelegate::paint(painter, option, index);
</code></pre>
<p dir="auto">Good grief, this is getting hard for one darned <code>%</code> character...! :(</p>
]]></description><link>https://forum.qt.io/post/594046</link><guid isPermaLink="true">https://forum.qt.io/post/594046</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Mon, 11 May 2020 15:33:17 GMT</pubDate></item><item><title><![CDATA[Reply to Model View alter display text on Mon, 11 May 2020 15:09:38 GMT]]></title><description><![CDATA[<p dir="auto">Hi<br />
Its not that much painter code to append %</p>
<pre><code>void ListviewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &amp;option,
                             const QModelIndex &amp;index) const
{


    QStyleOptionViewItem nonConstOption(option);
    initStyleOption(&amp;nonConstOption, index);
    int val = 66;// get from model
    nonConstOption.text = QString("%1%").arg(val);
    QApplication::style()-&gt;drawControl(QStyle::CE_ItemViewItem, &amp;nonConstOption, painter, nullptr);
}

</code></pre>
]]></description><link>https://forum.qt.io/post/594037</link><guid isPermaLink="true">https://forum.qt.io/post/594037</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Mon, 11 May 2020 15:09:38 GMT</pubDate></item><item><title><![CDATA[Reply to Model View alter display text on Mon, 11 May 2020 14:46:55 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/bonnie">@<bdi>Bonnie</bdi></a><br />
I wish!  I got beyond shouted out (nicely) by expert people here saying I should be boiled in oil :)  Not quite, but they were <em>very</em> firm on "don't change the return type of <code>Qt::DisplayRole</code>, it isn't intended for this at all, and the only time it should differ from <code>Qt::EditRole</code> is as an example where you are doing a spreadsheet and you have the display role showing a calculated result number while the edit role is the text of a formula".  A quite different type of situation.  For output like this the way to do is through a item delegate, only.  Model code must not concern with output format.  I posted an explicit question about just this and gracefully accepted their answers, and <em>promised</em> I would adhere to it next time I wrote code... :)</p>
<p dir="auto">Lemme see if I can find it:</p>
]]></description><link>https://forum.qt.io/post/594028</link><guid isPermaLink="true">https://forum.qt.io/post/594028</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Mon, 11 May 2020 14:46:55 GMT</pubDate></item><item><title><![CDATA[Reply to Model View alter display text on Mon, 11 May 2020 14:32:35 GMT]]></title><description><![CDATA[<p dir="auto">I'm not a expert but returning a QString looks fine to me. :)</p>
]]></description><link>https://forum.qt.io/post/594023</link><guid isPermaLink="true">https://forum.qt.io/post/594023</guid><dc:creator><![CDATA[Bonnie]]></dc:creator><pubDate>Mon, 11 May 2020 14:32:35 GMT</pubDate></item></channel></rss>