[Qt-interest] Why do I get a "" key in my map?
Jason H
scorp1us at yahoo.com
Tue Jun 8 20:09:09 CEST 2010
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