#include <iostream>
#include <QSettings>
#include <QString>
#include <QStringList>
using namespace std;

void writeSettings(){
    //第一个是路径,第二个是文件名,在linux下保存在./config/...下
    QSettings settings("QtGui/settings/","file1");
    settings.setValue("A",1);
    settings.setValue("B",2);
    settings.beginGroup("group1");
    settings.setValue("C","c");
    settings.setValue("D",4);
    //可以递归
    settings.beginGroup("group1.1");
    settings.setValue("E","e");
    settings.endGroup();
    settings.endGroup();
}

void readSettings(){
    QSettings settings("QtGui/settings/","file1");
    cout << "A:" << settings.value("A").toInt() << endl;
    cout << "B:" << settings.value("B").toInt() << endl;
    cout << "C:" << settings.value("/group1/C").toString().toStdString() << endl;
    cout << "D:" << settings.value("/group1/D").toInt() << endl;
    cout << "E:" << settings.value("/group1/group1.1/E").toString().toStdString() << endl;
    //给K默认值k,如果没读到就输出k
    cout << "K:" << settings.value("K","k").toString().toStdString() << endl;
    QStringList list = settings.allKeys();
    foreach(QString str,list){
        cout << str.toStdString() << " ";
    }
}

int main()
{
    writeSettings();
    readSettings();
}


A:1
B:2
C:c
D:4
E:e
K:k
A B group1/C group1/D group1/E group1/group1.1/E