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?