[Qt-interest] Why do I get a "" key in my map?

Dan Milburn danmilburn at clara.co.uk
Tue Jun 8 18:24:30 CEST 2010


Andreas Pakulat wrote:
> On 08.06.10 08:52:12, Jason H wrote:
>> void DlgEditVSN::setVSNs(QMap<QString, QString> VSNs)
>> {
>>     _VSNs = VSNs;
>>     while(ui.cmbSiteNames->count())
>>         ui.cmbSiteNames->removeItem(0);
>>
>>     foreach(const QString& site, _VSNs.keys())
>>     {
>>         ui.cmbSiteNames->addItem(site, _VSNs.value(site)); // THIS LINE MAKES A NULL KEY IN VSNs
> 
> There's a bit of context missing, in particular you didn't quite formulate
> a clear question. But QMap::value is documented to insert a
> default-constructed value if there is no one yet in the map for the given
> key. So its expected that value inserts into _VSNs if site doesn't exist in
> it yet.
> 
No, calling QMap::value just returns a default-constructed value if it 
doesn't exist in the map.  Using operator[] (the non-const version) will 
silently insert into the map.

Dan



More information about the Qt-interest-old mailing list