0

I am a bit lost with QVariantMAP/List and reference.

I load a json with QJson and convert it to QVariantMAP. currentJSON["tests"] is a QVariantList

I wish to browse currentJSON["tests"] and update the value of item["label"]. The first loop try to update the value, the second display it. Unfortunately the value display is not the updated value. I suppose this is a copy/reference problem but I do not find how to fix it.

QVariantMap currentJSON = jObject.toVariantMap(); //jobject is the json
QVariantList l = qvariant_cast<QVariantList>(currentJSON["tests"]);
for (QVariantList::iterator hehe = l.begin(); hehe != l.end(); hehe++) {
            QVariantMap test = hehe->toMap();
            test["label"].setValue(QVariant("AAAAAAAAAAAAAAAAAAA"));
}

l = qvariant_cast<QVariantList>(currentJSON["tests"]);
for (QVariantList::iterator hehe = l.begin(); hehe != l.end(); hehe++) {
            QVariantMap test = hehe->toMap();
            //the value print is not AAAAAAAAAAAAAAAAAAA
            qDebug() << test["label"].toString();
}

If you can help me, thank.

1 Answers1

0

Ok, with the help from Amartel I have found this solution:

 QVariantList l = qvariant_cast<QVariantList>(currentJSON["tests"]);
 for (QVariantList::iterator hehe = l.begin(); hehe != l.end(); hehe++) {
            //qDebug() << hehe->toMap();
            QVariantMap test = hehe->toMap();
            test["label"].setValue(QVariant("AAAAAAAAAAAAAAAAAAA"));
            *hehe = test;
 }
 currentJSON["tests"] = l;

 l = qvariant_cast<QVariantList>(currentJSON["tests"]);
 for (QVariantList::iterator hehe = l.begin(); hehe != l.end(); hehe++) {
            QVariantMap test = hehe->toMap();
            qDebug() <<"y " << test["label"].toString();
 }

I have added: *hehe = test; currentJSON["tests"] = l;

But this is a bit complicated if I have many lists nesteds. Is there a way to work with reference instead of copy ?

  • `Is there a way to work with reference instead of copy ?` - Doubt it. `QVariant` is a complicated container itself. It uses stream operators for serializing/deserializing values, so it cannot return you a reference. – Amartel Jul 27 '15 at 11:57