Warning "allocating an unneeded temporary container" when getting the index of a QMap key
-
I'm trying to get the index of a key in a
QMap
. I have this code:map.keys().indexOf(someKey);
This works fine, but I get a warning saying "allocating an unneeded temporary container", which suggests that there is a better way to do this. The warning doesn't say what I should do instead though. If searched for how to get the index of a
QMap
key, but I haven't found anything interesting.How do I fix this warning?
-
@Donald-Duck
Since your keys areQString
you could save yourself some time/hassle withcomboBox.setCurrentText(someKey)
:) -
@Donald-Duck said in Warning "allocating an unneeded temporary container" when getting the index of a QMap key:
How do I fix this warning?
By reading the docs. An an index of a map is nothing which you should work with.
-
@Christian-Ehrlicher The docs don't seem to have anything to get the index. The method you linked to gives the key of a given value. I already have the key, and I need the index associated with it as a number.
If you need to know why I need this, it's because I have a
QComboBox
which is supposed to show values corresponding to the keys:QMap<QString, NotImportant> map; QComboBox comboBox; foreach(const QString &key, map.keys()){ comboBox.addItem(key); } comboBox.setCurrentIndex(map.keys().indexOf(someKey)); //This is the line that gives the warning
Since the
setCurrentIndex
method takes anint
, I need the index associated with the key, not the key itself. I could always copymap.keys()
into aQList
, but that seems unnecessary. -
Again: you should and must not use an index of a map since this is nothing a map can be described with.
Since you already create a container you can use this:const auto keys = map.keys(); comboBox.addItems(keys); comboBox.setCurrentIndex(keys.indexOf(someKey));
-
@Donald-Duck
Since your keys areQString
you could save yourself some time/hassle withcomboBox.setCurrentText(someKey)
:)