Results 1 to 3 of 3

Thread: QSettings, my own format of config-file

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    Join Date
    Feb 2007
    Posts
    16
    Thanks
    2
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QSettings with XML format

    Here is my solution:

    Qt Code:
    1. bool readXmlFile( QIODevice& device, QSettings::SettingsMap& map )
    2. {
    3. QXmlStreamReader xmlReader( &device );
    4.  
    5. QString currentElementName;
    6. while( !xmlReader.atEnd() )
    7. {
    8. xmlReader.readNext();
    9. while( xmlReader.isStartElement() )
    10. {
    11. if( xmlReader.name() == "SettingsMap" )
    12. {
    13. xmlReader.readNext();
    14. continue;
    15. }
    16.  
    17. if( !currentElementName.isEmpty() )
    18. {
    19. currentElementName += "/";
    20. }
    21. currentElementName += xmlReader.name().toString();
    22. xmlReader.readNext();
    23. }
    24.  
    25. if( xmlReader.isEndElement() )
    26. {
    27. continue;
    28. }
    29.  
    30. if( xmlReader.isCharacters() && !xmlReader.isWhitespace() )
    31. {
    32. QString key = currentElementName;
    33. QString value = xmlReader.text().toString();
    34.  
    35. map[ key ] = value;
    36.  
    37. currentElementName.clear();
    38. }
    39. }
    40.  
    41. if( xmlReader.hasError() )
    42. {
    43. return false;
    44. }
    45.  
    46. return true;
    47. }
    48.  
    49. bool writeXmlFile( QIODevice& device, const QSettings::SettingsMap& map )
    50. {
    51. QXmlStreamWriter xmlWriter( &device );
    52. xmlWriter.setAutoFormatting( true );
    53.  
    54. xmlWriter.writeStartDocument();
    55. xmlWriter.writeStartElement( "SettingsMap" );
    56.  
    57. QSettings::SettingsMap::const_iterator mi = map.begin();
    58. for( mi; mi != map.end(); ++mi )
    59. {
    60. std::vector< std::string > groups;
    61. StringUtils::SplitList( mi.key().toStdString().c_str(), "/", &groups );
    62. foreach( std::string groupName, groups )
    63. {
    64. xmlWriter.writeStartElement( groupName.c_str() );
    65. }
    66.  
    67. xmlWriter.writeCharacters( mi.value().toString() );
    68.  
    69. foreach( std::string groupName, groups )
    70. {
    71. xmlWriter.writeEndElement();
    72. }
    73. }
    74.  
    75. xmlWriter.writeEndElement();
    76. xmlWriter.writeEndDocument();
    77.  
    78. return true;
    79. }
    To copy to clipboard, switch view to plain text mode 


    I just saw that QString has a Split method. Use that instead of my StringUtils::SplitList call.
    Last edited by joshlareau; 14th November 2007 at 21:55.

  2. The following 2 users say thank you to joshlareau for this useful post:

    emobemo (20th July 2017), rishid (19th January 2008)

Similar Threads

  1. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21
  2. Using QSettings to read pre-made INI file..
    By ShaChris23 in forum Newbie
    Replies: 1
    Last Post: 3rd May 2007, 05:36
  3. config file class
    By feizhou in forum Qt Programming
    Replies: 2
    Last Post: 23rd November 2006, 07:49
  4. Config file parsing
    By Vash5556 in forum Qt Programming
    Replies: 2
    Last Post: 10th September 2006, 23:11
  5. Replies: 13
    Last Post: 1st June 2006, 14:01

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.