0

I have a nested QMap QMap <QString, QMap<QString, QVariant> > map

and a temporary QMap QMap <QString, QVariant> tmpMap

I need to fill the temporary QMap with the inner QMap's keys and values so I can

loop through and output all the values of the nested QMap.

This is currently my code

QMap <QString, QMap<QString, QVariant> > map;
QMap <QString, QVariant> tmpMap;
QList<QString> mapKeys = map.keys();

for(int index = 0; index < mapKeys.size(); ++index)
{
  tmpMap.unite(map.value(QString(index)));
  QList<QString> tmpMapKeys = tmpMap.keys()

    for(int index2 = 0, index2 < tmpMapKeys.size(); ++index2)
    {
      //Stuff to check and output
    }
}

However, the second for loop never runs since the tmpMap never stores anything.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
TheIntern
  • 13
  • 7
  • How is original `map` filled? Did you try to inspect its content at the beginning of `for` loop? – mvidelgauz Jun 23 '16 at 19:19
  • The original map is filled in another function and contains everything when I pass it into the function I am using it in – TheIntern Jun 23 '16 at 19:28
  • I would still suggest to set beakpoint at `for` line, step over the code under debugger and watch values and contents of participants. – mvidelgauz Jun 23 '16 at 19:38

3 Answers3

1

Like platonshubin said, the key in the first loop isn't quite clear, instead of:

tmpMap.unite(map.value(QString(index)));

try

tmpMap.unite(map.value(mapKeys.at(index)));
kmx78
  • 55
  • 2
  • 10
1

The QString(index) doesn't do what you think it does. You might be thinking of QString::number(index), but even then it won't work if any of the keys have values that aren't numbers in the limited range you iterate on. You really should have used mapKeys.at(index): it'd get your code to work. But you shouldn't be copying the keys of the map into mapKeys at all: it's premature pessimization.

Thankfully, C++11' makes all of this easy and concise. You can use range-for to iterate the values - the inner maps - of map. And you can use a deduced-type const-iterator to then iterate the key/value pairs of the accumulated allInners map.

#include <QtCore>

int main() {
   QMap<QString, QMap<QString, QVariant>> map;
   QMap<QString, QVariant> allInners;

   // accumulate the inner keys/values
   for (auto const & inner : map)
      allInners.unite(inner);

   // process the inner keys/values
   for (auto it = allInners.cbegin(); it != allInners.cend(); ++it)
      qDebug() << it.key() << it.value();
}
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
0

What do you mean by writing map.value(QString(index))? This is quite ambiguous for me, at least. In example if I write QString(0x40) I will get a string with @ character in it (0x40 is its code) and you are doing the same, I guess, i.e. calling QString(int). So try to write more explicitly.

To iterate through some containers I'd recommend you to use iterators which are very useful. The usage is following (code is not tested, though):

QMap <QString, QMap<QString, QVariant> > map;
QMap <QString, QMap<QString, QVariant> >::const_iterator i;
QMap<QString, QVariant>::const_iterator i2;

for (i = map.begin(); i != map.end(); i++)
{
    for (i2 = i.value.begin(); i2 != i.value.end(); i2++)
    {
        //Access to all the keys and values of nested QMap here
        //i2.key() is some QString and i2.value() is some QVariant
        //Stuff to check and output
    }
}

AFAIK there are no usual indexes in things like QMap, QHash, etc. If you have some QMap then you need to know the exact string to get 'Something' according to certain QString. If "Hello, world" goes to Something smth then if you write map.value("Hello, world") you will get this 'smth' object. To check all of QMap entries use iterators which I mentioned above.

If you need to get something by its number then I'd recommend you to use standart arrays, lists, sets, queues, stacks, etc.

P.S. kmx78's way should also work for you.

  • At that line I am trying to assign to tmpMap the value in map located at index, but I don't exactly know how to write it to do so. I'm certain this is where the tmpMap is not storing the inner Qmap values. – TheIntern Jun 23 '16 at 19:56
  • It is really poor C++ style to declare loop induction variables outside of the loop. Furthermore, C++11 has been with us for half a decade now, so you should be using range-for where possible. – Kuba hasn't forgotten Monica Jun 23 '16 at 23:50