Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. Polish
  4. QJsonDocument, QJsonValue, QJsonObject: How To Change Value from File
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Polish
1 Posts 1 Posters 2.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    doozii
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved