<?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 print all member QMap with correct key]]></title><description><![CDATA[<p dir="auto">i need the correct key , how to fix my code , i'm using <code>devnum_bbu_slotid.key(slot_id)</code> for getting key, here's my snipet .</p>
<p dir="auto">thanks.</p>
<p dir="auto">data:</p>
<pre><code>QMap(("L1071905001", 6)("L1071905001", 3)("L1081905001", 7)("L1081905001", 6))
</code></pre>
<p dir="auto">code:</p>
<pre><code>QMap&lt;QString,int&gt; devnum_bbu_slotid;
foreach (int slot_id , devnum_bbu_slotid){
            qDebug()&lt;&lt;"-- key:"&lt;&lt;devnum_bbu_slotid.key(slot_id)&lt;&lt;" value:"&lt;&lt;slot_id;
}
</code></pre>
<p dir="auto">output :</p>
<pre><code>-- key: "L1071905001"  value: 6
-- key: "L1071905001"  value: 3
-- key: "L1081905001"  value: 7
-- key: "L1071905001"  value: 6
</code></pre>
]]></description><link>https://forum.qt.io/topic/119406/how-to-print-all-member-qmap-with-correct-key</link><generator>RSS for Node</generator><lastBuildDate>Fri, 12 Jun 2026 20:53:07 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/119406.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 28 Sep 2020 08:20:24 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How To print all member QMap with correct key on Mon, 28 Sep 2020 09:20:22 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/619306">How To print all member QMap with correct key</a>:</p>
<blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/bonnie">@<bdi>Bonnie</bdi></a><br />
The help page for the latest (<a href="https://doc.qt.io/qt-5/qmap.html" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-5/qmap.html</a>) has no <code>QMap::insertMulti</code>, and states</p>
<blockquote>
<p dir="auto">Normally, a QMap allows only one value per key. If you call insert() with a key that already exists in the QMap, the previous value will be erased. For example:<br />
However, you can store multiple values per key by using using the subclass QMultiMap. If you want to retrieve all the values for a single key, you can use values(const Key &amp;key), which returns a QList&lt;T&gt;:</p>
</blockquote>
<p dir="auto">So we wouldn't want to encourage the OP to use any undocumented/obsolete API, would we? ;-)</p>
</blockquote>
<p dir="auto">i'm using QC 4.11, its documented well</p>
<blockquote>
<p dir="auto"><code>QMap::iterator QMap::insertMulti(const Key &amp;key, const T &amp;value)</code><br />
Inserts a new item with the key key and a value of value.<br />
If there is already an item with the same key in the map, this function will simply create a new one. (This behavior is different from insert(), which overwrites the value of an existing item.)<br />
See also insert() and values().</p>
</blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/bonnie">@<bdi>Bonnie</bdi></a> said in <a href="/post/619307">How To print all member QMap with correct key</a>:</p>
<blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/rifky">@<bdi>rifky</bdi></a><br />
Right! Iterator is the first thing comes to my mind.<br />
But I think maybe you prefer <code>foreach</code> :)</p>
</blockquote>
<p dir="auto">yeah ,, i got 2 solution at the same time , i'll use your code ,, that's more readable for me :)</p>
<p dir="auto">solved, thanks <a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a>  and <a class="plugin-mentions-user plugin-mentions-a" href="/user/bonnie">@<bdi>Bonnie</bdi></a>  for the quick response and helping me</p>
]]></description><link>https://forum.qt.io/post/619312</link><guid isPermaLink="true">https://forum.qt.io/post/619312</guid><dc:creator><![CDATA[rifky]]></dc:creator><pubDate>Mon, 28 Sep 2020 09:20:22 GMT</pubDate></item><item><title><![CDATA[Reply to How To print all member QMap with correct key on Mon, 28 Sep 2020 09:08:47 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/619306">How To print all member QMap with correct key</a>:</p>
<blockquote>
<p dir="auto">The help page for the latest (<a href="https://doc.qt.io/qt-5/qmap.html" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-5/qmap.html</a>) has no QMap::insertMulti</p>
</blockquote>
<p dir="auto">It is in the <code>Obsolete members</code> page.</p>
<blockquote>
<p dir="auto">So we wouldn't want to encourage the OP to use any undocumented/obsolete API, would we? ;-)</p>
</blockquote>
<p dir="auto">Of course!<br />
I was just assuming the OP is already using it so he may think what you said is not true...</p>
]]></description><link>https://forum.qt.io/post/619309</link><guid isPermaLink="true">https://forum.qt.io/post/619309</guid><dc:creator><![CDATA[Bonnie]]></dc:creator><pubDate>Mon, 28 Sep 2020 09:08:47 GMT</pubDate></item><item><title><![CDATA[Reply to How To print all member QMap with correct key on Mon, 28 Sep 2020 09:04:11 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/rifky">@<bdi>rifky</bdi></a><br />
Right! Iterator is the first thing comes to my mind.<br />
But I think maybe you prefer <code>foreach</code> :)</p>
]]></description><link>https://forum.qt.io/post/619307</link><guid isPermaLink="true">https://forum.qt.io/post/619307</guid><dc:creator><![CDATA[Bonnie]]></dc:creator><pubDate>Mon, 28 Sep 2020 09:04:11 GMT</pubDate></item><item><title><![CDATA[Reply to How To print all member QMap with correct key on Mon, 28 Sep 2020 09:03:17 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/bonnie">@<bdi>Bonnie</bdi></a><br />
The help page for the latest (<a href="https://doc.qt.io/qt-5/qmap.html" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-5/qmap.html</a>) has no <code>QMap::insertMulti</code>, and states</p>
<blockquote>
<p dir="auto">Normally, a QMap allows only one value per key. If you call insert() with a key that already exists in the QMap, the previous value will be erased. For example:<br />
However, you can store multiple values per key by using using the subclass QMultiMap. If you want to retrieve all the values for a single key, you can use values(const Key &amp;key), which returns a QList&lt;T&gt;:</p>
</blockquote>
<p dir="auto">So we wouldn't want to encourage the OP to use any undocumented/obsolete API, would we? ;-)</p>
]]></description><link>https://forum.qt.io/post/619306</link><guid isPermaLink="true">https://forum.qt.io/post/619306</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Mon, 28 Sep 2020 09:03:17 GMT</pubDate></item><item><title><![CDATA[Reply to How To print all member QMap with correct key on Mon, 28 Sep 2020 09:01:40 GMT]]></title><description><![CDATA[<p dir="auto">Sorry for late respon, i try to change this code , i've found another solution, before i'm using <code>devnum_bbu_slotid.insertMulti</code></p>
<pre><code>QMultiMap&lt;QString,int&gt; devnum_bbu_slotid;
QMapIterator&lt;QString, int&gt; x(devnum_bbu_slotid);
 while (x.hasNext()) {
     x.next();
     qDebug()&lt;&lt;"-- key:"&lt;&lt;x.key()&lt;&lt; "value:" &lt;&lt;x.value();
 }
</code></pre>
<p dir="auto">it's worked</p>
]]></description><link>https://forum.qt.io/post/619305</link><guid isPermaLink="true">https://forum.qt.io/post/619305</guid><dc:creator><![CDATA[rifky]]></dc:creator><pubDate>Mon, 28 Sep 2020 09:01:40 GMT</pubDate></item><item><title><![CDATA[Reply to How To print all member QMap with correct key on Mon, 28 Sep 2020 09:00:49 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/619301">How To print all member QMap with correct key</a>:</p>
<blockquote>
<p dir="auto">if you are not using a QMultiMap (you don't seem to use that) you do not have any multiple values against a given key in a QMap</p>
</blockquote>
<p dir="auto">Actually he can...<br />
<code>QMap::insertMulti</code> is obsolete, but still exists...</p>
]]></description><link>https://forum.qt.io/post/619303</link><guid isPermaLink="true">https://forum.qt.io/post/619303</guid><dc:creator><![CDATA[Bonnie]]></dc:creator><pubDate>Mon, 28 Sep 2020 09:00:49 GMT</pubDate></item><item><title><![CDATA[Reply to How To print all member QMap with correct key on Mon, 28 Sep 2020 08:55:39 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/rifky">@<bdi>rifky</bdi></a><br />
As per <a class="plugin-mentions-user plugin-mentions-a" href="/user/bonnie">@<bdi>Bonnie</bdi></a>, if you are <em>not</em> using a <code>QMultiMap</code> (you don't seem to use that) you do not have any multiple values against a given key in a <code>QMap</code>: the last one added <em>replaces</em> the previous one.  If you do want multi-values per key, you must change over to <code>QMultiMap</code>, and then you can use <a class="plugin-mentions-user plugin-mentions-a" href="/user/bonnie">@<bdi>Bonnie</bdi></a>'s code.</p>
]]></description><link>https://forum.qt.io/post/619301</link><guid isPermaLink="true">https://forum.qt.io/post/619301</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Mon, 28 Sep 2020 08:55:39 GMT</pubDate></item><item><title><![CDATA[Reply to How To print all member QMap with correct key on Mon, 28 Sep 2020 08:49:14 GMT]]></title><description><![CDATA[<p dir="auto">Hey, I assume that you are using <code>insertMulti()</code> to add values since you have different values with the same key.<br />
That makes you <code>QMap</code> actually a <code>QMultiMap</code>.<br />
So referring to the <a href="https://doc.qt.io/qt-5/containers.html#the-foreach-keyword" target="_blank" rel="noopener noreferrer nofollow ugc">foreach</a> doc:</p>
<blockquote>
<p dir="auto">QMultiMap&lt;QString, int&gt; map;<br />
...<br />
foreach (const QString &amp;str, map.uniqueKeys()) {<br />
foreach (int i, map.values(str))<br />
qDebug() &lt;&lt; str &lt;&lt; ':' &lt;&lt; i;<br />
}</p>
</blockquote>
<p dir="auto">Your code should be</p>
<pre><code>foreach (const QString &amp;key, devnum_bbu_slotid.uniqueKeys()) {
    foreach (int slot_id, devnum_bbu_slotid.values(key))
        qDebug()&lt;&lt;"-- key:"&lt;&lt;key&lt;&lt;" value:"&lt;&lt;slot_id;
}
</code></pre>
]]></description><link>https://forum.qt.io/post/619299</link><guid isPermaLink="true">https://forum.qt.io/post/619299</guid><dc:creator><![CDATA[Bonnie]]></dc:creator><pubDate>Mon, 28 Sep 2020 08:49:14 GMT</pubDate></item><item><title><![CDATA[Reply to How To print all member QMap with correct key on Mon, 28 Sep 2020 08:46:27 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/rifky">@<bdi>rifky</bdi></a><br />
Ah, I see, with the keys you picked being so similar I didn't even notice that there are two different keys!  You might have drawn the reader's attention to this!</p>
<p dir="auto">So instead of using <code>key()</code> ("Returns the <em>first</em> key with value value") you need to call <a href="https://doc.qt.io/qt-5/qmap.html#keys-1" target="_blank" rel="noopener noreferrer nofollow ugc">QList&lt;Key&gt; QMap::keys(const T &amp;value) const</a>, which</p>
<blockquote>
<p dir="auto">Returns a list containing all the keys associated with value value in ascending order.</p>
<p dir="auto">This function can be slow (linear time), because QMap's internal data structure is optimized for fast lookup by key, not by value.</p>
</blockquote>
<p dir="auto">So you get a <em>list</em> of <em>all</em> the keys with that value, and do what you will with those.</p>
<p dir="auto">Or, don't try to lookup by value (you'll need to know all the values), just iterate the whole <code>QMap</code> and you will meet the multiple keys which (happen to) have the same value.  There is no "correct key" for a value of <code>6</code>, there are simply multiple keys with that value.</p>
]]></description><link>https://forum.qt.io/post/619296</link><guid isPermaLink="true">https://forum.qt.io/post/619296</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Mon, 28 Sep 2020 08:46:27 GMT</pubDate></item><item><title><![CDATA[Reply to How To print all member QMap with correct key on Mon, 28 Sep 2020 08:33:46 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/619294">How To print all member QMap with correct key</a>:</p>
<blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/rifky">@<bdi>rifky</bdi></a> said in <a href="/post/619292">How To print all member QMap with correct key</a>:</p>
<blockquote>
<p dir="auto">i need the correct key , how to fix my code</p>
</blockquote>
<p dir="auto">?  What do you mean?  You add duplicate keys into the <code>QMap</code>, and that is what your code prints out.  If you mean you want to know the key for, say, value of <code>7</code> then obviously just print out <code>devnum_bbu_slotid.key(7)</code>?</p>
</blockquote>
<p dir="auto">i mean,<br />
L1071905001 have 2 values and L1081905001 have 2 values ,<br />
i want data printed like this,</p>
<pre><code>-- key: "L1071905001"  value: 6
-- key: "L1071905001"  value: 3
-- key: "L1081905001"  value: 7
-- key: "L1081905001"  value: 6
</code></pre>
<p dir="auto">but this code print L1081905001 just have 1 value, because one of the values same(6) as L1071905001</p>
]]></description><link>https://forum.qt.io/post/619295</link><guid isPermaLink="true">https://forum.qt.io/post/619295</guid><dc:creator><![CDATA[rifky]]></dc:creator><pubDate>Mon, 28 Sep 2020 08:33:46 GMT</pubDate></item><item><title><![CDATA[Reply to How To print all member QMap with correct key on Mon, 28 Sep 2020 08:28:07 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/rifky">@<bdi>rifky</bdi></a> said in <a href="/post/619292">How To print all member QMap with correct key</a>:</p>
<blockquote>
<p dir="auto">i need the correct key , how to fix my code</p>
</blockquote>
<p dir="auto">?  What do you mean?  You add duplicate keys into the <code>QMap</code>, and that is what your code prints out.  If you mean you want to know the key for, say, value of <code>7</code> then obviously just print out <code>devnum_bbu_slotid.key(7)</code>?</p>
]]></description><link>https://forum.qt.io/post/619294</link><guid isPermaLink="true">https://forum.qt.io/post/619294</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Mon, 28 Sep 2020 08:28:07 GMT</pubDate></item></channel></rss>