Saving ComboBox data
-
Hello again.
Can You help me how to save data from ComboBox to file and after restart the program load them? I want to save the index, name, and kcals.
//save void MainWindow::on_saveButton_clicked() { QSettings settings("/Users/Alan/Desktop/QT Projects/Planner/save/save.ini", QSettings::IniFormat); { settings.setValue("MealIndex",ui->mealOneLeft->currentIndex()); settings.setValue("MealKcal", ItemKcals[ItemID] = ui->addItemKcal->value()); settings.setValue("MealName", ui->addItemName->text()); } } //load void MainWindow::on_loadButton_clicked() { QSettings settings("/Users/Alan/Desktop/QT Projects/Planner/save/save.ini"); int index = settings.value("MealIndex",+1).toInt(); ui->mealOneLeft->setCurrentIndex(index); settings.value("MealKcal").toInt(); ItemKcals[index]=ui->addItemKcal->value(); settings.value("MealName").toString(); ui->addItemName->text(); }
I have only this but I'm not sure that's the correct way to do that. Please don't flame me because of my knowledge of QT.
-
@naax
Assuming this is what you want to do. Some code is wrong, so this is only guesswork:settings.setValue("MealKcal", ItemKcals[ItemID] = ui->addItemKcal->value());
looks wrong, why an inline assignment? Sort this out.
You onload starts off promisingly, you restore via
int index = settings.value("MealIndex",+1).toInt(); ui->mealOneLeft->setCurrentIndex(index);
So you need the same approach for the other two, which you don't have right at the moment:
// next 2 lines I don't know because of how you saved int ItemID = settings.value("MealKcal").toInt(); ItemKcals[ItemID ]=something; QString mealName = settings.value("MealName").toString(); ui->addItemName->setText(mealName );
-
//save void MainWindow::on_saveButton_clicked() { QSettings settings("/Users/Alan/Desktop/QT Projects/Planner/save/save.ini", QSettings::IniFormat); // { <= not required settings.setValue("MealIndex",ui->mealOneLeft->currentIndex()); ItemKcals[ItemID] = ui->addItemKcal->value(); settings.setValue("MealKcal", ItemKcals[ItemID]); // <= easier to read IMHO settings.setValue("MealName", ui->addItemName->text()); // } <= not required } //load void MainWindow::on_loadButton_clicked() { QSettings settings("/Users/Alan/Desktop/QT Projects/Planner/save/save.ini"); int index = settings.value("MealIndex",+1).toInt(); ui->mealOneLeft->setCurrentIndex(index); int mealKcal = settings.value("MealKcal").toInt(); ItemKcals[index]=mealKcals; ui->addItemKcal->value(); // <= not clear wht this is. How would you store hardwired? QString meal = settings.value("MealName").toString(); ui->addItemName->text(); // <= not clear wht this is. How would you store hardwired? }
-
@naax
On a separate matter, I wouldn't save combobox stuff by saving/restoringcurrentIndex()
, personally. It relies on index number being the same, which can change over time. I would savecurrentText()
and restore viasetCurrentText()
, https://doc.qt.io/qt-5/qcombobox.html#currentText-prop. If you want to stick with yours as a beginner that's fine, I'm just making an observation. -
@JonB said in Saving ComboBox data:
@koahnig
I think you'll find OP has statements likeui->addItemName->text();
because he is not aware that Qt sets viasetText()
orsetValue()
etc., as per my suggestions for that line.... :)I wasn't up to anything to assume here. setText and setValue is a possibility, but ... ;)
-
-
@naax said in Saving ComboBox data:
So I need to save every single line or I can save ComboBox once with this all stuff inside?
It looks like your mixing things here.
You started asking how to save the selected option of a ComboBox (or at some extent many for what matters) but now you're asking to save all the options for all your ComboBox objects?
Why? If you already have the options in the source code already!You may need to decide if the options for the combobox objects will be changeable via data (coming from a text file, a settings file, a database) without changing your code, or if it's just Ok to hard-code the values and just only save the selected option(s)
-
@Pablo-J-
I thought about text file with basic Items with attributes, and load this all stuff to ComboBoxes with every start program. And if ill add another one, a new item will save to text file. Maybe this is a better way and "easier"? Did you know what I'm talking about? (Sorry my English is not enough)