开发者

Elegant non-const reference to a QVariantMap node?

开发者 https://www.devze.com 2023-01-12 23:43 出处:网络
Basically I need to generate a nested QVariantMap. (Think JSON): { \"foo开发者_运维百科\" : 1, \"bar\" : { \"node\" : 0 } }

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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消