QJsonDocument, QJsonValue, QJsonObject: How To Change Value from File



  • Hello again! I would love some help. I have a file that contains some JSON values and the file acts as a persistent store when values get changed inside my program. I have my program reading the values in just fine but I'm running into a little trouble when I try to change the values.

    The QT Docs are a little lacking and even the sparse examples I've found don't really address my particular problem. What I want to do is this: When a value that I want to store is changed, I want to update that particular key in the json file without having to change the entire file (every other key value).

    I'm using QT Creator 5.2.1 in Windows 7.

    This is what I've tried:
    @
    QFile configFile("MyConfig.json");
    QString inputData;

    if(configFile.open(QIODevice::ReadWrite | QIODevice::Text))
    {
        inputData = configFile.readAll();
        QJsonDocument jsonDoc = QJsonDocument::fromJson(inputData.toUtf8());  // This gets the document
        QJsonObject jo = jsonDoc.object(); // This converts the document into an object
        QJsonValue value = jo.value(QString("myValue")); // Contains the value I want to update before writing
    
      value = 10;  //  some Variable Value that I want to push back to the file
    

    ...
    @

    When I try this, I get a compile error message "error C2248: 'QJsonValue::QJsonValue' : cannot access private member declared in class 'QJsonValue'".

    So my question is how do I change the value of this QJsonValue? And will changing this value still allow me to write the entire JSON document back to the file with the new value?

    Thanks!


Log in to reply
 

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