Problem with my WriteFunc for QSettings
-
Hi everyone ! I'm here because I have some problems with WriteFunc for QSettings. I'm trying to use a XML file for QSettings but my problem is that the QIODevice given by the function call is empty.
Worse, every time my function is call, the file is erased.
My function :
@bool writeXML(QIODevice &device, const QSettings::SettingsMap &map)
{
QTextStream out(&device);
QDomDocument doc;QMap<QString, QVariant>::iterator i = map.begin(); for(i; i != map.end(); ++i) { if(i.key() == "HardMode") { QDomElement HardMode = doc.createElement("HardMode"); QDomElement time = doc.createElement("time"); time.appendChild(doc.createTextNode(i.value().toString())); HardMode.appendChild(time); doc.appendChild(HardMode); } else { QStringList groupAndTable = i.key().split("/"); QDomElement Mode = doc.createElement(groupAndTable.at(0)); QDomElement time = doc.createElement("time"); time.setAttribute("table", groupAndTable.at(1)); time.appendChild(doc.createTextNode(i.value().toString())); Mode.appendChild(time); doc.appendChild(Mode); } } doc.save(out, 4); return true;
}
@I believed that Qt managed the "merge" between datas in file and datas which was written in the empty QIODevice given by the function call or that Qt gave the device pointing on the file with datas but no ! ^^
So now, I ask you for know how do it.P.S : Don't take attention about my English... I'm french.
-
Hi,
your writeFunc MUST write all data in file, no merge are performed.
-
Every time I wanna to add some values to the file or just modified a value, I have to rewrite all data without the file content ?
Thank you for your interest =)
-
[quote author="keke222" date="1367780127"]Every time I wanna to add some values to the file or just modified a value, I have to rewrite all data without the file content ?
[/quote]The WriteFunc is used by QSettings to write file, so you MUST re-write full file contents on it.
-
I thought that we could do what Qt does with the ini files : create a file and insert some key/value pairs in this file and we could modify or add new key/value pairs in the file... I'll have to make a custom class to do it so. Thank you for your help !