views:

78

answers:

1

Basically I need to generate a nested QVariantMap. (Think JSON):

{ "foo" : 1, "bar" : { "node" : 0 } }

I do this in Qt this way:

QVariantMap r, r_bar;

r["foo"] = QVariant(1);
r_bar["node"] = QVariant(0);

r["bar"] = r_bar;

Which is very inconvenient for large nested structures. Is there an elegant way of doing this, such as this (fake syntax)?:

r["foo"] = QVariant(1);
r["bar"]["node"] = QVariant(0);

I cannot do this:

r["foo"].toMap()["node"] = QVariant(0)

because all "toSomething" functions of QVariant return a copy of the object, not a reference to it.

Can anyone help me with this?

Thanks!

+1  A: 

You could use only one map like this:

r["foo"] = QVariant(1);
r["bar/node"] = QVariant(0);

The only problem with this approach is that you lose the ability to iterate sub maps. There is no easy way to find out the subnodes of the "bar" node.

Roku