[SOLVED] Update value in QJsonObject



  • Hi,

    I have a QTreeView that represents a QJsonObject. When I change a value in the tree view, I'd like to update the corresponding value in the QJsonObject.

    I tried using myJsonObject.value(key) method to find the value by it's key, but this only works for key:value pairs in the top level of the tree. How can I find a key:value pair that is not on the top level? If I must iterate over it, how can I know if an element in the JSON map is a node or a leaf?

    Thanks!



  • Let me reformulate the question:

    I have a QJsonObject that has this structure:
    @{
    "A":"a",
    "G1":{
    "X":"x"
    },
    "G2":{
    "X":"x"
    },
    "G3":{
    "Y":"y"
    }
    }@

    myJsonObject.find("A") returns "a", but myJsonObject.find("Y") returns "undefined" instead of "y". Same for myJsonObject.value("Y"), or myJsonObject.values("X").

    How can I find "X" or "Y" in such a structure?

    I tried converting QJsonObject to QVariantMap, with the same result.

    Cheers.


  • Lifetime Qt Champion

    Hi,

    Since you have a tree, you could follow the branches to get to the value.
    i.e you know that X is under G2. You can rebuild the path from your model data (or are you using QTreeWidget ?)

    Hope it helps



  • I'm using a QTreeView.

    Your solution makes sense. I'll give it a try. Thanks a lot!


Log in to reply
 

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