PDA

View Full Version : QSettings size does not change on adding new array element



Amita
21st July 2011, 14:32
Dear All,
I am using QSettings for creating and adding config file. And I am using Array element. I am adding and deleting array elements programatically. I am able to add/delete that array element from config file but doing so it does not change the size of the array. Next time I run the program I cannot see the changes in size.

sizeOfArray = pSettings->beginReadArray("ArrayName");

This is because adding or deleting elements in the config file does not change the value "size __ " automatically. Does anybody know how to change the size of QSettings Array. Currently I am doing it manually by reading the file using QFile, but I don't like it.

Please let me know if you know something in this regard.

Thank you in advance,
Amita

mcosta
21st July 2011, 17:37
This is because adding or deleting elements in the config file does not change the value "size __ " automatically.

What do you means for "adding or deleting elements in the config file"?

For me it works.

This code


void EditorWidget::writeSettings()
{
QSettings settings;

QTextDocument *doc = ui->plainTextEdit->document ();
QTextBlock block;

settings.beginGroup ("editor");
settings.beginWriteArray ("contents");
for (int i = 0; i < doc->blockCount (); ++i) {
settings.setArrayIndex (i);
block = doc->findBlockByNumber (i);
settings.setValue ("text", block.text ());
}

settings.endArray ();
settings.endGroup ();
}

save the contents of a QPlainTextEdit line by line

alexisdm
21st July 2011, 17:51
Hi,

QSettings doesn't really support editing arrays, and doesn't even check that the indexes are contiguous. It just takes the maximum index passed to setArrayIndex, adds 1, and writes it as the size.
To have a clean array without leftovers or hole between the indexes, you have to read it to memory, remove the whole array and rewrite it entirely.

Amita
22nd July 2011, 14:48
Thank you guys for the answer. At the end I also did the same. Whenever I need to edit the array, I am deleting it completely and rewriting it.