<?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[How to know if the QHash::insert(const Key &amp;key, const T &amp;value) succeeded or failed]]></title><description><![CDATA[<p dir="auto">The <a href="http://doc.qt.io/qt-5/qhash.html#insert" target="_blank" rel="noopener noreferrer nofollow ugc">documentation</a> says nothing about this.</p>
]]></description><link>https://forum.qt.io/topic/80064/how-to-know-if-the-qhash-insert-const-key-key-const-t-value-succeeded-or-failed</link><generator>RSS for Node</generator><lastBuildDate>Tue, 22 Sep 2026 01:12:00 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/80064.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 08 Jun 2017 03:37:59 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to know if the QHash::insert(const Key &amp;key, const T &amp;value) succeeded or failed on Thu, 08 Jun 2017 05:16:46 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/eman.lived">@<bdi>eMan.Lived</bdi></a> Well, in case memory cannot be allocated there will be an exception and the application will be closed by the OS.</p>
]]></description><link>https://forum.qt.io/post/398070</link><guid isPermaLink="true">https://forum.qt.io/post/398070</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Thu, 08 Jun 2017 05:16:46 GMT</pubDate></item><item><title><![CDATA[Reply to How to know if the QHash::insert(const Key &amp;key, const T &amp;value) succeeded or failed on Thu, 08 Jun 2017 05:15:26 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/venkatesh-v">@<bdi>Venkatesh-V</bdi></a><br />
Hello,<br />
It looks like the developers have forgotten something. I mean that it looks more clear like this:</p>
<pre><code>QHash&lt;key, value&gt; hash;
if (hash.insert(Key, Value) == hash.end())
{
  // failed
}
else
{
  // succeeded
}
</code></pre>
<p dir="auto">Well, thanks for the workaround.</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jsulm">@<bdi>jsulm</bdi></a><br />
Hello,<br />
I've looked <a href="http://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/tools/qhash.h" target="_blank" rel="noopener noreferrer nofollow ugc">source code</a>.</p>
<ol>
<li><code>QHash::insert()</code> just return result of called <code>QHash::insertMulti()</code> (line 1011).</li>
<li><code>QHash::insertMulti()</code>, in turn, returns an iterator, in the constructor of which the result of calling function <code>createNode()</code> is passed (line 781).</li>
<li><code>createNode()</code> returns a pointer to the <code>Node</code> created by the <code>new</code> operator (lines 548, 551).</li>
</ol>
<p dir="auto">As you can see there is no check for successful memory allocation. Maybe throws an exception?</p>
]]></description><link>https://forum.qt.io/post/398067</link><guid isPermaLink="true">https://forum.qt.io/post/398067</guid><dc:creator><![CDATA[eMan.Lived]]></dc:creator><pubDate>Thu, 08 Jun 2017 05:15:26 GMT</pubDate></item><item><title><![CDATA[Reply to How to know if the QHash::insert(const Key &amp;key, const T &amp;value) succeeded or failed on Thu, 08 Jun 2017 04:35:50 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/eman.lived">@<bdi>eMan.Lived</bdi></a> insert() returns an iterator. I'm not sure (and documentation really does not say anything about it!), but I think if insertion fails the iterator will be hash.end(), otherwise it will point to the just inserted pair. You can check Qt source code to find out what really happens.</p>
]]></description><link>https://forum.qt.io/post/398055</link><guid isPermaLink="true">https://forum.qt.io/post/398055</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Thu, 08 Jun 2017 04:35:50 GMT</pubDate></item><item><title><![CDATA[Reply to How to know if the QHash::insert(const Key &amp;key, const T &amp;value) succeeded or failed on Thu, 08 Jun 2017 04:07:43 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/eman.lived">@<bdi>eMan.Lived</bdi></a><br />
Hi,</p>
<p dir="auto">Based on QHash size you can know value is inserted or not.<br />
if inserted size becomes +1  to previous size if not size remains same as previous size.</p>
<p dir="auto">int mapSize;<br />
QHash&lt;key,value&gt; hash;<br />
mapSize = hash.size();</p>
<p dir="auto">hash.insert(key,Value);<br />
if(hash.size()==mapSize+1)<br />
{<br />
//inserted succeeded<br />
mapSize = hash.size();</p>
<p dir="auto">}else{<br />
//insertFailed<br />
}</p>
]]></description><link>https://forum.qt.io/post/398052</link><guid isPermaLink="true">https://forum.qt.io/post/398052</guid><dc:creator><![CDATA[Venkatesh V]]></dc:creator><pubDate>Thu, 08 Jun 2017 04:07:43 GMT</pubDate></item></channel></rss>