Problem with saving arrays with QSettings, may be a bug
-
Hello guys,could you help me finding the problems?really i don't understand where the problem is, the save part is surely correct since the output file is exactly as the documentation states but as soon as i try to load the setting file nothing gets loaded since the size of SettingsObject.beginReadArray("DllPaths") is always 0,got any idea?? thanks in advice..
@void Options::closeEvent(QCloseEvent *event)
{
QString SettingsPath = QApplication::applicationDirPath() + "/Settings/Options";
if (!QDir(SettingsPath).exists())
QDir().mkdir(SettingsPath);QSettings SettingsObject(SettingsPath + "/OptionSettings.ini",QSettings::IniFormat);
SettingsObject.beginGroup("MapleStoryPath");
SettingsObject.setValue("PathMS",ui.lineDir->text());
SettingsObject.endGroup();SettingsObject.beginWriteArray("DllPaths");
for (int i = 0; i < DllList.size(); i++)
{
SettingsObject.setArrayIndex(i);
SettingsObject.setValue("DllName", DllList.at(i).DllName);
SettingsObject.setValue("DllPath", DllList.at(i).DllPath);
}
SettingsObject.endArray();
}void Options::LoadSettings()
{
QList<DllStruct> ListToLoad;
QString MySettings = QApplication::applicationDirPath() + "/Settings/Options/OptionSettings.ini";
QSettings SettingsObject(MySettings,QSettings::IniFormat);SettingsObject.beginGroup("MapleStoryPath");
QString MaplePath = SettingsObject.value("PathMS","").toString();
ui.lineDir->setText(MaplePath);
SettingsObject.endGroup();DllStruct DllEntry;
for (int i = 0; i < SettingsObject.beginReadArray("DllPaths") ; i++)
{
SettingsObject.setArrayIndex(i);
DllEntry.DllName = SettingsObject.value("DllName").toString();
DllEntry.DllPath = SettingsObject.value("DllPath").toString();
ListToLoad.append(DllEntry);
}if (!ListToLoad.isEmpty())
AddDll(ListToLoad);SettingsObject.endArray();
}void Options::AddDll(QList<DllStruct> ListToLoad)
{
QTreeWidget* Tree = this->ui.treeWidgetDlls;
QTreeWidgetItem* TreeItems = new QTreeWidgetItem(Tree);for (int i = 0; i < ListToLoad.size(); i++)
{
TreeItems->setText(0, ListToLoad.at(i).DllName);
TreeItems->setText(1, ListToLoad.at(i).DllPath);
this->ui.treeWidgetDlls->addTopLevelItem(TreeItems);
}
}
@ -
I would suggest to move SettingsObject.beginReadArray("DllPaths") out of loop. Something like this:
@
int size = SettingsObject.beginReadArray("DllPaths");
for (int i = 0; i < size; ++i)
{
SettingsObject.setArrayIndex(i);
DllEntry.DllName = SettingsObject.value("DllName").toString();
DllEntry.DllPath = SettingsObject.value("DllPath").toString();
ListToLoad.append(DllEntry);
}
@