<?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[Individual formatting of a QTableWidget&#x27;s column]]></title><description><![CDATA[<p dir="auto">Hi all :-)</p>
<p dir="auto">I have a <code>QTableWidget</code> I individualized using a <code>QStyledItemDelegate</code> so that the editor for one column becomes a <code>QDoubleSpinBox</code> instead of a <code>QLineEdit</code> (as in this column, a price should be entered and not text).</p>
<p dir="auto">The problem is that if one e.g. enters "5,00 €" in the spin box, I still get "5" after pressing enter, or "5,5" for "5,50 €".</p>
<p dir="auto">I searched how to change this, but it seems that changing the DisplayRole of some data requires to implement an own <code>QTableView</code> using a custom <code>QAbstractTableModel</code>. Is this the case? I only need to do this one thing, everything else would be fine how the <code>QTableWidget</code> does it ;-)</p>
<p dir="auto">Thanks for all clarification!</p>
]]></description><link>https://forum.qt.io/topic/143788/individual-formatting-of-a-qtablewidget-s-column</link><generator>RSS for Node</generator><lastBuildDate>Sun, 09 Aug 2026 15:06:57 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/143788.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 21 Mar 2023 16:23:39 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Individual formatting of a QTableWidget&#x27;s column on Tue, 21 Mar 2023 17:54:40 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> Oh, I didn't know you could install a delegate for just one column. I think that will work then, I'll try this. Thanks for the hint :-)</p>
]]></description><link>https://forum.qt.io/post/751768</link><guid isPermaLink="true">https://forum.qt.io/post/751768</guid><dc:creator><![CDATA[l3u_]]></dc:creator><pubDate>Tue, 21 Mar 2023 17:54:40 GMT</pubDate></item><item><title><![CDATA[Reply to Individual formatting of a QTableWidget&#x27;s column on Tue, 21 Mar 2023 17:55:37 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/l3u_">@<bdi>l3u_</bdi></a><br />
I had already appended to my previous answer for this.</p>
<p dir="auto">First confirm which of the two you want.  Let's guess it's the first, you want it stored as a number and just want to control the output format.  Then the simplest is to use <code>QStyledItemDelegate::displayText()</code>.</p>
<p dir="auto">To make this operate only on a given column.  Either create your own <code>class DoubleStyledItemDelegate: public QStyledItemDelegate</code> and install on just that column via <a href="https://doc.qt.io/qt-6/qabstractitemview.html#setItemDelegateForColumn" target="_blank" rel="noopener noreferrer nofollow ugc">QAbstractItemView::setItemDelegateForColumn()</a>.  Or in a common <code>QStyledItemDelegate</code> you would override <code>QStyledItemDelegate::paint()</code>, which is passed the model index to identify the column, and produce the desired text to print there (example at, say, <a href="https://stackoverflow.com/questions/34729858/override-text-in-qstyleditemdelegate-for-qtreeview" target="_blank" rel="noopener noreferrer nofollow ugc">https://stackoverflow.com/questions/34729858/override-text-in-qstyleditemdelegate-for-qtreeview</a>).  If you can, the first alternative seems preferable and re-usable.</p>
]]></description><link>https://forum.qt.io/post/751766</link><guid isPermaLink="true">https://forum.qt.io/post/751766</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Tue, 21 Mar 2023 17:55:37 GMT</pubDate></item><item><title><![CDATA[Reply to Individual formatting of a QTableWidget&#x27;s column on Tue, 21 Mar 2023 17:38:51 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> Yes, exactly. The entered text is displayed formatted by the <code>QDoubleSpinBox</code>. I'm totally aware of the fact the the value is of course a naked <code>double</code> in this case, no matter how I format the spin box.</p>
<p dir="auto">My question was if it's possble to tweak what's displayed by the <code>QTableWidget</code> for one column (i.e. by changing the <code>DisplayRole</code>) without having to implement the whole thing.</p>
<p dir="auto">I can't use a <code>QSortFilterProxyModel</code>, as setting a model is private for a <code>QTableWidget</code>. I can't use <code>QStyledItemDelegate::displayText</code> either, because there's no way to see from which column the data comes …</p>
<p dir="auto">Edit:</p>
<p dir="auto">Oh, I cross-posted or you cross-edited. Well, as said, I can't use <code>QStyledItemDelegate::displayText</code>. Well, storing the actual text would be a possibility, but I would have to process the text later on, to get the double value back, because I use the data entered later …</p>
]]></description><link>https://forum.qt.io/post/751762</link><guid isPermaLink="true">https://forum.qt.io/post/751762</guid><dc:creator><![CDATA[l3u_]]></dc:creator><pubDate>Tue, 21 Mar 2023 17:38:51 GMT</pubDate></item><item><title><![CDATA[Reply to Individual formatting of a QTableWidget&#x27;s column on Tue, 21 Mar 2023 17:34:27 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/l3u_">@<bdi>l3u_</bdi></a> said in <a href="/post/751755">Individual formatting of a QTableWidget's column</a>:</p>
<blockquote>
<p dir="auto">The problem is that if one e.g. enters "5,00 €" in the spin box, I still get "5" after pressing enter, or "5,5" for "5,50 €".</p>
</blockquote>
<p dir="auto">It's a double spin box for entering a floating point number.  <code>5,00 == 5</code> and <code>5,50 == 5,5</code> as numbers.  Where exactly are you seeing the <em>text</em> you are quoting?</p>
<p dir="auto">Do you mean when the editing spin box has gone, the value the user entered has been stored as data and the <code>QTableWidget</code> is displaying that as uneditable text?</p>
<p dir="auto">If this the case then you have two choices:</p>
<ul>
<li>
<p dir="auto">If you want that column to always be shown as, say, 2 decimal places with a euro symbol you can store it as a number like <code>QDoubleSpiinBox::value()</code> gives you, and have the <code>QStyledItemDelegate</code> output it like that always (see <code>displayText()</code>).  What the user actually typed is ignored.</p>
</li>
<li>
<p dir="auto">If you want that column to show what the user actually typed into the spin box, not just its value converted to a <code>double</code>, so you keep e.g. how many trailing <code>0</code>s were typed or whether a currency symbol was entered, then you would have to store the result not as a number but as text.  For that the base class has <a href="https://doc.qt.io/qt-6/qabstractspinbox.html#text-prop" target="_blank" rel="noopener noreferrer nofollow ugc">QAbstractSpinBox::text()</a>.</p>
</li>
</ul>
]]></description><link>https://forum.qt.io/post/751761</link><guid isPermaLink="true">https://forum.qt.io/post/751761</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Tue, 21 Mar 2023 17:34:27 GMT</pubDate></item></channel></rss>