PDA

View Full Version : Saving settings to XML



arturo182
7th March 2009, 22:40
Hi.

I'm new to QT, I found this forum very helpful, but now I have a problem that I couldn't find a solution for in old topics.

I'm trying to to save QSettings to XML, I know I have to register a new format, I did all that, I got readXMLfile and writeXMLfile functions from this post (http://www.qtcentre.org/forum/p-qsettings-my-own-format-of-config-file-post54105/postcount3.html), changed them a bit and everything works just fine, except one thing.

If I want two values with same parent element names, they duplicate in XML file, let me give you an example:

the code is:


settings->setValue("themes/author/name", "arturo182");
settings->setValue("themes/author/email", "arturo182@qwerty.com");


xml file looks like this:


<?xml version="1.0" encoding="UTF-8"?>
<settings>
<themes>
<author>
<name>arturo182</name>
</author>
</themes>
<themes>
<author>
<email>arturo182@qwerty.com</email>
</author>
</themes>
</settings>


and what I'm trying to achieve is:


<?xml version="1.0" encoding="UTF-8"?>
<settings>
<themes>
<author>
<name>arturo182</name>
<email>arturo182@qwerty.com</email>
</author>
</themes>
</settings>


I tried almost everything, but as I said, I'm new to QT and not sure how to fix this problem.
Maybe someone has better idea of how to achive what I want to do.
I already checked QDomDocument, QDomElement and all that, but I just love creating XML by using syntax like "parent/child/childchild".

Also it would be nice to have access to attributes like this:


setValue("themes/author[name]", "arturo182");

caduel
8th March 2009, 09:36
well, you must not close all the group-tags

foreach( std::string groupName, groups )
{
xmlWriter.writeStartElement( groupName.c_str() );
}

xmlWriter.writeCharacters( mi.value().toString() );

foreach( std::string groupName, groups )
{
xmlWriter.writeEndElement();
}


instead, you need to store the "current" group path in a variable.
Then calculate the path for the next setting: close only the tags (list suffix) no longer in the new path; open the new suffix path tags...

I once posted something like that in http://www.qtcentre.org/forum/f-qt-programming-2/t-qabstractitemmodel-subclass-duplicate-entries-14453.html (http://www.qtcentre.org/forum/f-qt-programming-2/t-qabstractitemmodel-subclass-duplicate-entries-14453.html/?highlight=path)

HTH

arturo182
8th March 2009, 11:10
Yes that could work if I had similiar paths next to each other, but if I had something like this:



settings->setValue("themes/author/name", "arturo182");
settings->setValue("something/else", "test");
settings->setValue("themes/author/email", "arturo182@qwerty.com");


I think I should sort the SettingsMap by key before saving, is there a way to do it with one of QT's functions or do I have to do it by myself?