0

In short, what is happening is I am making QStringList object and then manually adding a entry at the front called "Item Types:". Then I am taking data from the JSON file and adding it to the QStringList. Then using the addItems function for a qcombobox to make the QStringList be selection options for the user.

My issue comes when I call the addItems line. It does add the items from the QStringList to the combobox, but when that line is called it also adds "Item Types:" (that was manually added) to my JSON data. This is what is confusing to me and I am not sure why this is happening. I will go into more detail below.

I have a JSON file that has some information that must be read in for my program to work correctly. I am using JSON by nlohmann to parse the data from the file. The code below reads the data in and puts the JSON data in the variable js.

using json = nlohmann::json;
static json js;

void readJsonSettings()
{
    std::ifstream ifs("C:/Dev/Qt/myproject/Settings.txt");
    js = json::parse(ifs);
}; 

My JSON file will look like this:

{
Test:
{
Arr_Item:[1,2,3],
Sub_Item:
    {
    Item1 : [0,1,2,3],
    Item2 : [4,5,6,7],
    }
}
}

I am also using QT 6 for building a GUI for the user. The code for this looks something like below:

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    readJsonSettings();
    
    QStringList itemKeyList;
    itemKeyList << "Item Types:";
    getKeys(itemKeyList , "Sub_Item");


    ui->item_comboBox->addItems(itemKeyList);
}

MainWindow::~MainWindow()
{
    delete ui;
}

The getKeys function looks like this:

void getKeys(QStringList &outKeyList, const char* lookUpKey, const std::string profileName = currentProfileName)
{

    for (auto it = js[profileName][lookUpKey].begin(); it != js[profileName][lookUpKey].end(); ++it)
    {
        outKeyList << QString::fromStdString(it.key());
    }
}

I know the profileName variable might be confusing but just know the user can select different profiles that will load in different types of data based on the user choice but isn't relevant to the question I have. For this example the profileName will be "Test"

For this example the getKeys function will return a list of "Item Types:","Item1","Item2". Then when the ui->item_comboBox->addItems(itemKeyList); line is called my JSON data that is stored in js turns into:

{
Test:
{
Arr_Item:[1,2,3],
Sub_Item:
    {
    Item Types:[],
    Item1 : [0,1,2,3],
    Item2 : [4,5,6,7],
    }
}
}

After that long explanation, my main question is how/why would calling a QT function change the data that is associated with the JSON variable? How do I change it so that it does not have an effect on it?

laxer
  • 720
  • 11
  • 41
  • I don't understand why you wonder about the fact that the first element is "Item Types:" because you add it to the QStringList by yourself before adding the rest of them in getKeys() ... – chehrlic Jul 14 '21 at 17:29
  • @chehrlic yes, but I added it to the QStringList, not the JSON data. The JSON data being changed is what I am confused about. Why would using the Qt function change the data in the nlohmann/JSON data variable? – laxer Jul 14 '21 at 17:35
  • You did not show where or how you store the data... – chehrlic Jul 15 '21 at 14:37
  • @chehrlic in the bit of code above the `readJsonSettings` function there is a `static json js` that is declared. Within that `readJsonSettings` function it stores all the data in that `js` variable. Then In the MainWindow I am declaring `QStringList itemKeyList;` which is holding all the keys that the `getKeys` function finds within the `js` variable. – laxer Jul 15 '21 at 14:51
  • 2
    Please provide a minimal, compilable example to reproduce the problem. – chehrlic Jul 15 '21 at 18:11
  • How did you check the content of `js`? The JSON data looks the same to me before and after – Minh Jul 16 '21 at 05:46

0 Answers0