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

Dan Milburn danmilburn at clara.co.uk
Wed Jun 9 10:25:44 CEST 2010


I have no idea, I was just correcting Andreas.  I would suggest stepping 
through the code in a debugger to determine where the insert is coming from.

By the way, this is a very inefficient way to use QMap.  I would suggest 
writing your loop as something like (use QMapIterator if you prefer):

for( QMap<QString,QString>::const_iterator it = VSNs.begin(), endIt = 
VSNs.end(); it != endIt; ++it )
{
   ui.cmbSiteNames->addItem( it.key(), it.value() );
}


Dan

> But given that 'site' id obtained from keys(), how could I ever ask for an item not in the map?
> 
> 
> 
> ----- Original Message ----
> From: Dan Milburn <danmilburn at clara.co.uk>
> To: qt-interest at trolltech.com
> Sent: Tue, June 8, 2010 12:24:30 PM
> Subject: Re: [Qt-interest] Why do I get a "" key in my map?
> 
> 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
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
> 
> 
> 
>       



More information about the Qt-interest-old mailing list