<?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[lose precesion when qtreeview show double value]]></title><description><![CDATA[<p dir="auto">my json contains double value like 32.6743408794, but when i load and show it ,it only shows 32.67。When debug in QJsonTreeItem* QJsonTreeItem::load(const QJsonValue&amp; value, QJsonTreeItem* parent) fuction , i found the value in child is correct actually, so i think it's a treeview widget problem not jsonmodel. If someone could give me some advice, i'll appreciate. ：)<br />
<img src="https://user-images.githubusercontent.com/35841685/224885981-26b2ac1f-6b2d-405f-8b12-047e3a4d3532.PNG" alt="alt text" class=" img-fluid img-markdown" /><br />
<img src="https://user-images.githubusercontent.com/35841685/224885987-06b4f956-8de0-4c38-aacd-2df570e72efc.PNG" alt="alt text" class=" img-fluid img-markdown" /></p>
]]></description><link>https://forum.qt.io/topic/143644/lose-precesion-when-qtreeview-show-double-value</link><generator>RSS for Node</generator><lastBuildDate>Fri, 24 Apr 2026 03:35:51 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/143644.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 15 Mar 2023 07:30:54 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to lose precesion when qtreeview show double value on Wed, 15 Mar 2023 12:59:21 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/chris-kawa">@<bdi>Chris-Kawa</bdi></a> thank for ur explaination. after custom my delegate , i solved this problem.<img src="https://s1.ax1x.com/2023/03/15/pp3lToj.jpg" alt="alt text" class=" img-fluid img-markdown" /></p>
<p dir="auto">and when click double value item,  should reimplement setEditorData function to specify the format,otherwise the double value will shrink too. below is my key code,hope it helps someone else.</p>
<pre><code>QWidget* XtrTaskViewDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem&amp; option, const QModelIndex&amp; index) const
{
	return QStyledItemDelegate::createEditor(parent, option, index);
}

void XtrTaskViewDelegate::setEditorData(QWidget* editor, const QModelIndex&amp; index) const
{
	if (index.data().type() == QVariant::Double)
	{
		QDoubleSpinBox* doubleSpinBox = static_cast&lt;QDoubleSpinBox*&gt;(editor);

		doubleSpinBox-&gt;setDecimals(10);
		doubleSpinBox-&gt;setValue(index.data().toDouble());
	}
	else 
	{
		QStyledItemDelegate::setEditorData(editor, index);
	}
}
</code></pre>
]]></description><link>https://forum.qt.io/post/751107</link><guid isPermaLink="true">https://forum.qt.io/post/751107</guid><dc:creator><![CDATA[WuYaZi]]></dc:creator><pubDate>Wed, 15 Mar 2023 12:59:21 GMT</pubDate></item><item><title><![CDATA[Reply to lose precesion when qtreeview show double value on Wed, 15 Mar 2023 10:14:56 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a> Well yes, but also no :) <code>data()</code> returns a QVariant, which does not need to be a string. In case of numbers it's often preferable to return the actual type, like double or int and let the view display it appropriately. This allows to have different views display the data from the same model differently e.g. have one view display a rounded number and another (e.g. a "detailed view") to show the full thing. You <strong>could</strong> do the conversion in the model, but it limits your further options and formatting shouldn't really be the model's job I think. The way I interpret <code>DisplayRole</code> is "the data to be displayed", not "how to display the data".</p>
]]></description><link>https://forum.qt.io/post/751092</link><guid isPermaLink="true">https://forum.qt.io/post/751092</guid><dc:creator><![CDATA[Chris Kawa]]></dc:creator><pubDate>Wed, 15 Mar 2023 10:14:56 GMT</pubDate></item><item><title><![CDATA[Reply to lose precesion when qtreeview show double value on Wed, 15 Mar 2023 10:01:40 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/chris-kawa">@<bdi>Chris-Kawa</bdi></a> Or simply return the correct formated value in data() for Qt::DisplayRole (this is what this role is for).</p>
]]></description><link>https://forum.qt.io/post/751090</link><guid isPermaLink="true">https://forum.qt.io/post/751090</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Wed, 15 Mar 2023 10:01:40 GMT</pubDate></item><item><title><![CDATA[Reply to lose precesion when qtreeview show double value on Wed, 15 Mar 2023 09:51:38 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/wuyazi">@<bdi>WuYaZi</bdi></a> QTreeView by default uses QStyledItemDelegate to paint its items and that uses the locale object to get the display text from the double value returned from the model data. See <a href="https://codebrowser.dev/qt6/qtbase/src/widgets/itemviews/qabstractitemdelegate.cpp.html#573" target="_blank" rel="noopener noreferrer nofollow ugc">source</a>. By default the precision is set to 6 digits, so either your particular locale overrides that or you have a custom item delegate that does the double to string conversion differently.</p>
<p dir="auto">In any case you can <a href="https://doc.qt.io/qt-6/qabstractitemview.html#setItemDelegate" target="_blank" rel="noopener noreferrer nofollow ugc">provide a custom delegate</a> if you want to override the way it displays numbers.</p>
]]></description><link>https://forum.qt.io/post/751086</link><guid isPermaLink="true">https://forum.qt.io/post/751086</guid><dc:creator><![CDATA[Chris Kawa]]></dc:creator><pubDate>Wed, 15 Mar 2023 09:51:38 GMT</pubDate></item><item><title><![CDATA[Reply to lose precesion when qtreeview show double value on Wed, 15 Mar 2023 08:23:53 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a><br />
thank for ur reply.<br />
but i didn't want to change the type of such numberic value, cause i want to edit them through gui, if i return string , then when i save the json , a stringToDouble function will be added. I just wonder wheather there is a simpler way :</p>
]]></description><link>https://forum.qt.io/post/751083</link><guid isPermaLink="true">https://forum.qt.io/post/751083</guid><dc:creator><![CDATA[WuYaZi]]></dc:creator><pubDate>Wed, 15 Mar 2023 08:23:53 GMT</pubDate></item><item><title><![CDATA[Reply to lose precesion when qtreeview show double value on Wed, 15 Mar 2023 07:54:55 GMT]]></title><description><![CDATA[<p dir="auto">Fix your QJsonModel::data() function to return a proper string for Qt::DisplayRole.</p>
]]></description><link>https://forum.qt.io/post/751081</link><guid isPermaLink="true">https://forum.qt.io/post/751081</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Wed, 15 Mar 2023 07:54:55 GMT</pubDate></item><item><title><![CDATA[Reply to lose precesion when qtreeview show double value on Wed, 15 Mar 2023 07:42:15 GMT]]></title><description><![CDATA[<p dir="auto">btw, i use <a href="https://github.com/dridk/QJsonModel" target="_blank" rel="noopener noreferrer nofollow ugc">https://github.com/dridk/QJsonModel</a> for my json model  ,it's easy to use, but this problem annoy me.</p>
]]></description><link>https://forum.qt.io/post/751080</link><guid isPermaLink="true">https://forum.qt.io/post/751080</guid><dc:creator><![CDATA[WuYaZi]]></dc:creator><pubDate>Wed, 15 Mar 2023 07:42:15 GMT</pubDate></item></channel></rss>