[QSettings] how to update an array?
-
Hello,
I created settings which contain an array of models, each model having some properties, for example, property1 property2...
At some point, I want to modify properties of one of the entries in models.
I found that I can find the index of the entry, and withbeginWriteArray("Models")like here:
index: int = 2 n: int = self.settings.beginWriteArray("Models") self.settings.setArrayIndex(index) self.settings.setValue("language", language) self.settings.setValue("name", name) self.settings.endArray()this works except that after that, the size of the array is set such that my entry is then the last one. The following entries are still present, but ignored because of the size. I have for example three entries 1, 2, 3, but the size is 2 if I edited the 2 one.
Do I something wrong (yes, of course)?
Is there another method, except read all, delete all and write it again? -
Hi,
I am currently unsure about the behaviour of the begin/end write array methods. In between, what happens if you move the index through all the entries and change just the one in the middle ?
-
Hi,
I am currently unsure about the behaviour of the begin/end write array methods. In between, what happens if you move the index through all the entries and change just the one in the middle ?
@SGaist Thanks for your suggestion.
In the mean time, I had a look inthe tracker and found that I missed to specify the number of records in the call tobeginWriteArray:
https://bugreports.qt.io/browse/QTBUG-23944
Thus, the valid code is:index: int = 2 n: int = self.settings.beginReadArray("Models") self.settings.endArray() self.settings.beginWriteArray("Models", n) self.settings.setArrayIndex(index) self.settings.setValue("language", language) self.settings.setValue("name", name) self.settings.endArray() -
So you have it working now ?