Hi all,
I would like to store some data structured as an array. That's working, and my settings file contains something like:
[blogAccounts]
1\accountName=Test 1
1\userName=Mike
1\userPassword=secret
2\accountName=Test 2
2\userName=Mike
2\userPassword=secret
size=2
[blogAccounts]
1\accountName=Test 1
1\userName=Mike
1\userPassword=secret
2\accountName=Test 2
2\userName=Mike
2\userPassword=secret
size=2
To copy to clipboard, switch view to plain text mode
Now I use this code to remove the first item, and let's assume that the currentAccountName contains the first entry of the array:
...
// Ok, we need to find the right account...
int size = settings.beginReadArray("blogAccounts");
bool didRemove = false;
for (int i = 0; i < size; ++i)
{
// Select the data set
settings.setArrayIndex(i);
// Now compare the account name with the one selected...
if (_currentAccountName == settings.value("accountName").toString())
{
// This is the one we need to remove...
settings.endArray();
// Now reopen the settings for writing...
settings.beginWriteArray("blogAccounts");
// select the desired account again
settings.setArrayIndex(i);
// and now remove it...
settings.remove("");
didRemove = true;
break;
}
}
settings.endArray();
if (false == didRemove)
return; // Unable to remove account
...
// Ok, we need to find the right account...
QSettings settings("MKrueger", "MKBlog");
int size = settings.beginReadArray("blogAccounts");
bool didRemove = false;
for (int i = 0; i < size; ++i)
{
// Select the data set
settings.setArrayIndex(i);
// Now compare the account name with the one selected...
if (_currentAccountName == settings.value("accountName").toString())
{
// This is the one we need to remove...
settings.endArray();
// Now reopen the settings for writing...
settings.beginWriteArray("blogAccounts");
// select the desired account again
settings.setArrayIndex(i);
// and now remove it...
settings.remove("");
didRemove = true;
break;
}
}
settings.endArray();
if (false == didRemove)
return; // Unable to remove account
To copy to clipboard, switch view to plain text mode
The result in my settings file is this:
[blogAccounts]
2\accountName=Test 2
2\userName=Mike
2\userPassword=secret
size=1
[blogAccounts]
2\accountName=Test 2
2\userName=Mike
2\userPassword=secret
size=1
To copy to clipboard, switch view to plain text mode
If I now try to load the remaining account it fails. I suspect because the prefix is still 2 and not 1. Would that assumption be correct?
If so, how should I remove an item correctly? Do I have to remove the complete array and then write it out again?
Thanks...
Bookmarks