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

Tony Rietwyk tony.rietwyk at rightsoft.com.au
Wed Jun 9 14:09:51 CEST 2010


Jason 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
>     }
> 
>     if (_VSNs.keys().count())
>     {
>         _currentSite= ui.cmbSiteNames->itemText(0);
>         ui.cmbSiteNames->setCurrentIndex(0);
>     }
> }
> 
> Initial data:
> VSNs= (("???????",""))
> cmbSiteNames is a QComboBox. It should have one item of 
> "????????" with value of "". 
> 
> Ending data:
> VSNs= (("", ""), ("???????",""))
> 
> Thanks!

Do you have any slots connected to the combobox that update the map?  They
will get called with an empty string, or -1 index value, when you empty the
list.  This will occur in the while loop after the last item is removed, and
before the while loop tests the condition and stops.  

I suggest to add "qDebug() << _VSNs;" between the two loops and confirm what
is in the map there.  

Problems like this with slots mean that most of the forms and widgets in my
application have a bool that is set before updating all the child widgets,
which the slots test for and just exit if set.  Unfortunately, blockSignals
is too painful to use when there are lots of child widgets.  

Hope that helps, 

Tony.





More information about the Qt-interest-old mailing list