View Full Version : QMap

5th May 2009, 10:21
Hi, I want to modify the value in a QMap, but when I tried to modify it through QMap::value(), I failed, because this method always return a value which is const(cannot be changed).
Do you guys know how to change the value in QMap??

5th May 2009, 10:28
You can modify the value using [] operator. Some like this

map["one"] = 1;
map["three"] = 3;
map["seven"] = 7;

5th May 2009, 10:36
Thanks, but if I am using QMap in this way, QMap<int, QMap<QString, QToolButton *> >, then how can I add QToolButton into the inner QMap??

5th May 2009, 10:42
Like this

map[1]["one"] = toolButton

5th May 2009, 10:59
I use this, but the application crashed:

QMap<QString, QToolButton *> *currentButtons = const_cast<QMap<QString, QToolButton *>*>(tabs.value(ui->tabWidget->currentIndex()));
currentButtons->insert(path, btn); //This line is where the error lie

I am really puzzled.
Did I fail to const_cast the const into non-cosnt so that I cannot insert a value into a const variable??

25th May 2009, 11:05
There is an iterator for map.