Results 1 to 3 of 3

Thread: Saving settings to XML

  1. #1
    Join Date
    Mar 2009
    Location
    Belchatow, Poland
    Posts
    38
    Thanks
    11
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Saving settings to XML

    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, 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:
    Qt Code:
    1. settings->setValue("themes/author/name", "arturo182");
    2. settings->setValue("themes/author/email", "arturo182@qwerty.com");
    To copy to clipboard, switch view to plain text mode 

    xml file looks like this:
    Qt Code:
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <settings>
    3. <themes>
    4. <author>
    5. <name>arturo182</name>
    6. </author>
    7. </themes>
    8. <themes>
    9. <author>
    10. <email>arturo182@qwerty.com</email>
    11. </author>
    12. </themes>
    13. </settings>
    To copy to clipboard, switch view to plain text mode 

    and what I'm trying to achieve is:
    Qt Code:
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <settings>
    3. <themes>
    4. <author>
    5. <name>arturo182</name>
    6. <email>arturo182@qwerty.com</email>
    7. </author>
    8. </themes>
    9. </settings>
    To copy to clipboard, switch view to plain text mode 

    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:
    Qt Code:
    1. setValue("themes/author[name]", "arturo182");
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Saving settings to XML

    well, you must not close all the group-tags
    Qt Code:
    1. foreach( std::string groupName, groups )
    2. {
    3. xmlWriter.writeStartElement( groupName.c_str() );
    4. }
    5.  
    6. xmlWriter.writeCharacters( mi.value().toString() );
    7.  
    8. foreach( std::string groupName, groups )
    9. {
    10. xmlWriter.writeEndElement();
    11. }
    To copy to clipboard, switch view to plain text mode 

    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

    HTH

  3. #3
    Join Date
    Mar 2009
    Location
    Belchatow, Poland
    Posts
    38
    Thanks
    11
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: Saving settings to XML

    Yes that could work if I had similiar paths next to each other, but if I had something like this:

    Qt Code:
    1. settings->setValue("themes/author/name", "arturo182");
    2. settings->setValue("something/else", "test");
    3. settings->setValue("themes/author/email", "arturo182@qwerty.com");
    To copy to clipboard, switch view to plain text mode 

    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?

Similar Threads

  1. Using windows proxy settings
    By schall_l in forum Qt Programming
    Replies: 0
    Last Post: 22nd October 2008, 15:05
  2. saving settings does not work
    By MarkoSan in forum Qt Programming
    Replies: 4
    Last Post: 13th June 2008, 19:29
  3. QWidget Settings advanced idea
    By MarkoSan in forum Qt Programming
    Replies: 9
    Last Post: 27th March 2008, 09:41
  4. Where are these settings stored?
    By adorp in forum Qt Programming
    Replies: 6
    Last Post: 24th July 2007, 22:39
  5. Replies: 4
    Last Post: 1st February 2006, 17:17

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.