<?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[QSettings and float numeric type do not play nice together]]></title><description><![CDATA[<p dir="auto">I am attempting to read and write float values from/to a text file.  Using C++.<br />
The data type within the software is defined as:  'typedef float Float32_t;'  I inherited this and<br />
it is not changeable.</p>
<p dir="auto">Results in the text file look like this:  "@QVariant(\0\0\0\x87=\xcc\xcc\xcd", which is not acceptable.</p>
<p dir="auto">Here is the 'write' method:</p>
<p dir="auto">void writeCustom(const QString &amp;Key, const Float32_t &amp;val)<br />
{<br />
QSettings settings(mFilePath, QSettings::IniFormat);<br />
settings.setValue(Key, QVariant(static_cast&lt;float&gt;(val)));<br />
}</p>
<p dir="auto">This same approach works for double types.<br />
Please weigh in.  Thanks</p>
]]></description><link>https://forum.qt.io/topic/164391/qsettings-and-float-numeric-type-do-not-play-nice-together</link><generator>RSS for Node</generator><lastBuildDate>Thu, 23 Apr 2026 17:18:08 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/164391.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 06 Mar 2026 16:34:11 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QSettings and float numeric type do not play nice together on Sat, 07 Mar 2026 20:54:15 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/axel-spoerl">@<bdi>Axel-Spoerl</bdi></a> beat me to it, but quit messing with QVariant, then read/write your INI file as text, since INI is traditionally suppose to be a human readable format in the first place...and use normal stream read/write operations in text mode.  This is also important since text mode negates possible issues with endianness of numerical storage.</p>
]]></description><link>https://forum.qt.io/post/836939</link><guid isPermaLink="true">https://forum.qt.io/post/836939</guid><dc:creator><![CDATA[Kent-Dorfman]]></dc:creator><pubDate>Sat, 07 Mar 2026 20:54:15 GMT</pubDate></item><item><title><![CDATA[Reply to QSettings and float numeric type do not play nice together on Sat, 07 Mar 2026 10:36:10 GMT]]></title><description><![CDATA[<p dir="auto"><code>QSettings</code> is a pretty robust class which has its weaknesses.<br />
IMHO it tries to trade off type safety versus human readability.<br />
If I need waterproof numeric types in settings, never store them directly.<br />
Either I transform them to a string (which is also human readable).<br />
If I need it 100% safe, I use the <code>QDataStream</code> &lt;&lt; and &gt;&gt; operators and store the result in settings as a <code>QByteArray</code>.<br />
That's not human readable, but 100% type safe.</p>
<blockquote>
<p dir="auto">Qt version 5.2.1</p>
</blockquote>
<p dir="auto">I think Noah used this version in his shipyard, and it was outdated when he set sails....<br />
How about 6.8 for instance?</p>
]]></description><link>https://forum.qt.io/post/836932</link><guid isPermaLink="true">https://forum.qt.io/post/836932</guid><dc:creator><![CDATA[Axel Spoerl]]></dc:creator><pubDate>Sat, 07 Mar 2026 10:36:10 GMT</pubDate></item><item><title><![CDATA[Reply to QSettings and float numeric type do not play nice together on Fri, 06 Mar 2026 17:39:21 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/wareagle">@<bdi>wareagle</bdi></a><br />
You did <em>not</em> show the full code, because although we saw the final output we did not see what the <code>float</code> value serialized actually was.  Which means I cannot repro it.  We also saw Christian's complete example, which did work.</p>
<blockquote>
<p dir="auto">Qt version 5.2.1.</p>
</blockquote>
<p dir="auto">Are you aware just how old that version is (a decade)?  Nobody here will be able to try on that, I suspect?  That may be your final answer as to why it does not work, and you may have to use <code>double</code> if that works with that version.</p>
<p dir="auto">At least as of 7 years ago see <a href="https://stackoverflow.com/questions/55044655/why-is-qtsettings-storing-floats-in-a-weird-mode-what-am-missing" target="_blank" rel="noopener noreferrer nofollow ugc">https://stackoverflow.com/questions/55044655/why-is-qtsettings-storing-floats-in-a-weird-mode-what-am-missing</a> .  For your version that is the "answer" --- or someone's best guess at the <em>reason</em>.  Sounds like it <em>may</em> have changed in more recent versions.</p>
]]></description><link>https://forum.qt.io/post/836915</link><guid isPermaLink="true">https://forum.qt.io/post/836915</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Fri, 06 Mar 2026 17:39:21 GMT</pubDate></item><item><title><![CDATA[Reply to QSettings and float numeric type do not play nice together on Fri, 06 Mar 2026 17:10:10 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> My fall back is doing this processing as a type double, which I already know works.  My original question relates to why does float not work?  Which no one seems to know.  Is this a known issue with QSettings or what?  I showed the exact code and the exact output within the file.  Qt version 5.2.1.  Platform is RHEL 8 Virtual Machine within VMWare.</p>
]]></description><link>https://forum.qt.io/post/836913</link><guid isPermaLink="true">https://forum.qt.io/post/836913</guid><dc:creator><![CDATA[wareagle]]></dc:creator><pubDate>Fri, 06 Mar 2026 17:10:10 GMT</pubDate></item><item><title><![CDATA[Reply to QSettings and float numeric type do not play nice together on Fri, 06 Mar 2026 17:05:49 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/wareagle">@<bdi>wareagle</bdi></a><br />
Could you <em>please</em> change <a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a> 's example to use <code>0.1</code> as the value.  Show us the verbatim code and the output.  Use <code>0.1</code> for the <code>double</code> too.  Then wait for someone like Christian (not me) to re-test.</p>
<p dir="auto">Please do this before dealing with reading text and converting back, if that is what you do.  One thing at a time!  Let's see <em>only</em> what the problem writing a float to a <code>.ini</code> is, standalone.  Then, depending, show the shortest possible complete, standalone code demonstrating your issue.</p>
<p dir="auto">If you don't care about this and <code>double</code>s do work, don't forget like Christian said you can save as a double and deal with assigning it to <code>float</code> on reading.</p>
]]></description><link>https://forum.qt.io/post/836912</link><guid isPermaLink="true">https://forum.qt.io/post/836912</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Fri, 06 Mar 2026 17:05:49 GMT</pubDate></item><item><title><![CDATA[Reply to QSettings and float numeric type do not play nice together on Fri, 06 Mar 2026 16:59:28 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> The 'starting' value in the text file was 0.1.  After reading and then writing it to file it showed the '@Variant..... (I guess binary value, not sure) in the file.</p>
]]></description><link>https://forum.qt.io/post/836911</link><guid isPermaLink="true">https://forum.qt.io/post/836911</guid><dc:creator><![CDATA[wareagle]]></dc:creator><pubDate>Fri, 06 Mar 2026 16:59:28 GMT</pubDate></item><item><title><![CDATA[Reply to QSettings and float numeric type do not play nice together on Fri, 06 Mar 2026 16:57:16 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/wareagle">@<bdi>wareagle</bdi></a><br />
You're not supposed to do this, but OK :)  So please recheck by trying <a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a> 's example <em>verbatim</em>, where he is showing <code>float</code>s being stored readably.  Try with other values, like whatever your value was, to make sure.  If you don't find same result as him please report your Qt version &amp; platform.</p>
]]></description><link>https://forum.qt.io/post/836910</link><guid isPermaLink="true">https://forum.qt.io/post/836910</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Fri, 06 Mar 2026 16:57:16 GMT</pubDate></item><item><title><![CDATA[Reply to QSettings and float numeric type do not play nice together on Fri, 06 Mar 2026 16:47:40 GMT]]></title><description><![CDATA[<p dir="auto">The only real requirement is that the float value must be human readable in order for a user to be able to change it within the file.  Like =&gt; 0.1234</p>
]]></description><link>https://forum.qt.io/post/836908</link><guid isPermaLink="true">https://forum.qt.io/post/836908</guid><dc:creator><![CDATA[wareagle]]></dc:creator><pubDate>Fri, 06 Mar 2026 16:47:40 GMT</pubDate></item><item><title><![CDATA[Reply to QSettings and float numeric type do not play nice together on Fri, 06 Mar 2026 16:47:01 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 />
If you have</p>
<pre><code>float flt = 1.0f;
settings.setValue("flt", flt);
// stored in a .ini file as
flt=1
</code></pre>
<p dir="auto">then I don't know where/why the OP comes up with <code>@QVariant(\0\0\0\x87=\xcc\xcc\xcd</code> in the output/<code>.ini</code>/text file in the first place?  Unless by any chance it depends on the value in <code>flt</code> rather than a simple <code>1</code>.</p>
]]></description><link>https://forum.qt.io/post/836907</link><guid isPermaLink="true">https://forum.qt.io/post/836907</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Fri, 06 Mar 2026 16:47:01 GMT</pubDate></item><item><title><![CDATA[Reply to QSettings and float numeric type do not play nice together on Fri, 06 Mar 2026 16:41:55 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> said in <a href="/post/836905">QSettings and float numeric type do not play nice together</a>:</p>
<blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a><br />
I agree, but his problem will be that it will deserialize as a <code>QVariant(double)</code> where he presumably requires it to be <code>QVariant(float)</code>?  So he may need to do some massaging.</p>
</blockquote>
<p dir="auto">I don't understand - QSettings returns a QVariant. QVariant has toFloat()...</p>
<p dir="auto">And this works fine for me with Qt6.11</p>
<pre><code>double dbl = 1.0;
float flt = 1.0f;
QSettings settings("D:/tmp.ini", QSettings::IniFormat);
settings.setValue("dbl", dbl);
settings.setValue("flt", flt);
</code></pre>
<p dir="auto">--&gt;</p>
<pre><code>[General]
dbl=1
flt=1
</code></pre>
]]></description><link>https://forum.qt.io/post/836906</link><guid isPermaLink="true">https://forum.qt.io/post/836906</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Fri, 06 Mar 2026 16:41:55 GMT</pubDate></item><item><title><![CDATA[Reply to QSettings and float numeric type do not play nice together on Fri, 06 Mar 2026 16:40:16 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 />
I agree, but his problem will be that it will deserialize as a <code>QVariant(double)</code> where he presumably requires it to be <code>QVariant(float)</code>?  So he may need to do some massaging.</p>
]]></description><link>https://forum.qt.io/post/836905</link><guid isPermaLink="true">https://forum.qt.io/post/836905</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Fri, 06 Mar 2026 16:40:16 GMT</pubDate></item><item><title><![CDATA[Reply to QSettings and float numeric type do not play nice together on Fri, 06 Mar 2026 16:38:40 GMT]]></title><description><![CDATA[<p dir="auto">Also when double works fine - simply store them as double.</p>
]]></description><link>https://forum.qt.io/post/836904</link><guid isPermaLink="true">https://forum.qt.io/post/836904</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Fri, 06 Mar 2026 16:38:40 GMT</pubDate></item><item><title><![CDATA[Reply to QSettings and float numeric type do not play nice together on Fri, 06 Mar 2026 16:38:45 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/wareagle">@<bdi>wareagle</bdi></a> said in <a href="/post/836901">QSettings and float numeric type do not play nice together</a>:</p>
<blockquote>
<p dir="auto">Results in the text file look like this: "@QVariant(\0\0\0\x87=\xcc\xcc\xcd", which is not acceptable.</p>
</blockquote>
<p dir="auto">So that I understand.  What is "not acceptable"?  Assuming that value is correct, and deserializes correctly, that is all <code>Settings</code> is supposed to do.  Are you saying something like you want to impose requirements on its output (to a file or registry) such that it is human-readable to you?</p>
]]></description><link>https://forum.qt.io/post/836902</link><guid isPermaLink="true">https://forum.qt.io/post/836902</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Fri, 06 Mar 2026 16:38:45 GMT</pubDate></item></channel></rss>