View Full Version : Accessing Multiple Levels of QVariantMap

6th June 2013, 11:56
I wanted to create multidimensional arrays in QT.
I am trying to use QVariantMap to do so :

QVariantMap v;
v["lang"] = 1;
v["test"] = 1;

The above code works, but when I try to create another level of array, it doesnt work.

QVariantMap v;
v["test"]["test"] = 1;

How should I get this to work in QT ?
I need this functionality to insert as well as retrieve data from the Map.
I know its possible to create another QVariantMap first and then insert it into the MAP again :

QVariantMap v;
QVariantMap test;
test["a"] = 1;
test["b"] = 2;
v["test"] = test;

However in the above code, I am not able to modify the values of the nested map again like v["test"]["b"] = 3;

Is there anyway to achieve this ?

7th June 2013, 15:26
I have been thinking if I can inherit the QVariant class and modify the [] operator.
I am not an expert with QT classes and hence if anyone can guide me on this, it will be great.

7th June 2013, 23:35
The problem you have is because of the fact that QVariantMap is really a QMap<QString,QVariant> therefore calling v["x"] returns a QVariant and not a QVariantMap so it doesn't have the index operator.

8th June 2013, 11:43
It could be helpful to know what task you are trying to solve. Maybe there is a more suitable way doing it than QVariantMap