Guest User

QSettings with xml format

a guest
Apr 26th, 2010
2,461
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. static bool readXmlFile(QIODevice &device, QSettings::SettingsMap &map)
  2. {
  3.     qDebug()<< "read";
  4.     QXmlStreamReader reader(&device);
  5.     QString key;
  6.     while(!reader.atEnd())
  7.     {
  8.         if( reader.isStartElement() && reader.tokenString() != "Settings")
  9.         {
  10.             if( reader.text().isNull() )
  11.             {
  12.                 // key = Settings
  13.                 if(key.isEmpty())
  14.                     key = reader.tokenString();
  15.                 // key = Settings/Intervall
  16.                 else key += "/" + reader.tokenString();
  17.             }
  18.             else map.insert(key, reader.text().data());
  19.         }
  20.     }
  21.  
  22.     return true;
  23. }
  24.  
  25. static bool writeXmlFile(QIODevice &device, const QSettings::SettingsMap &map)
  26. {
  27.     qDebug()<< "write";
  28.     QXmlStreamWriter writer(&device);
  29.     writer.writeStartDocument("1.0");
  30.     writer.writeStartElement("Settings");
  31.     foreach(QString key, map.keys())
  32.     {
  33.         foreach(QString elementKey, key.split("/"))
  34.         {
  35.             writer.writeStartElement(elementKey);
  36.         }
  37.         writer.writeCDATA(map.value(key).toString());
  38.         writer.writeEndElement();
  39.     }
  40.     writer.writeEndElement();
  41.     writer.writeEndDocument();
  42.  
  43.     return true;
  44. }
  45.  
  46.  
  47.  
  48. int main(int argc, char *argv[])
  49. {
  50.     QApplication a(argc, argv);
  51.     //a.setApplicationName("SPC");
  52.     //a.setOrganizationName("EUP");
  53.  
  54.     const QSettings::Format XmlFormat = QSettings::registerFormat("xml", readXmlFile, writeXmlFile);
  55.     QSettings::setPath(XmlFormat, QSettings::UserScope, a.applicationFilePath());
  56.     QSettings::setDefaultFormat(XmlFormat);
  57.  
  58.     QSettings setting(XmlFormat, QSettings::UserScope, "EUP");
  59.     setting.setValue("xmlFormat", 2);
RAW Paste Data