QJsonObject insert QJsonObject error after remove()



  • i find that when i use QJsonObject.insert() and pass a QJsonObject (which call remove() before) as parament,it can;t work well. i think is more clear to post the code here:

            QJsonObject modulesObj = mainObj.take(QLatin1String("modules")).toObject();
            modulesObj.remove(QString::number(tempItem->moduleID));
            qDebug()<<modulesObj;
            qDebug()<<mainObj;
            mainObj.insert(QLatin1String("modules"),modulesObj);
            qDebug()<<mainObj<<(QJsonValue)modulesObj;
    

    like this ,the output will be wrong:

    QJsonObject({"0":{"info":"balabala..","input":{"0":""},"name":"test1","output":{"0":""}}})
    QJsonObject({})
    QJsonObject({"modules":{"":null}}) QJsonValue(object, QJsonObject({"0":{"info":"balabala..","input":{"0":""},"name":"test1","output":{"0":""}}}) )
    

    and if i delete the line use remove():

    QJsonObject mainObj = myProJsonDoc.object();
            QJsonObject modulesObj = mainObj.take(QLatin1String("modules")).toObject();
            //modulesObj.remove(QString::number(tempItem->moduleID));
            qDebug()<<modulesObj;
            qDebug()<<mainObj;
            mainObj.insert(QLatin1String("modules"),modulesObj);
            qDebug()<<mainObj<<(QJsonValue)modulesObj;
    

    this time it went well:

    QJsonObject({"0":{"info":"balabala..","input":{"0":""},"name":"test1","output":{"0":""}},"1":{"info":"balabala..","input":{"0":""},"name":"","output":{"0":""}}})
    QJsonObject({})
    QJsonObject({"modules":{"0":{"info":"balabala..","input":{"0":""},"name":"test1","output":{"0":""}},"1":{"info":"balabala..","input":{"0":""},"name":"","output":{"0":""}}}}) QJsonValue(object, QJsonObject({"0":{"info":"balabala..","input":{"0":""},"name":"test1","output":{"0":""}},"1":{"info":"balabala..","input":{"0":""},"name":"","output":{"0":""}}}) )
    

    i can't figure out why ,please help me.thank you .


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.