#include <QCoreApplication>
#include <QDebug>
#include <QDir>
#include <QDate>
#include <QSettings>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
/*设置公司名,架构名
在unix上,如果文件格式是NativeFormat, 则默认从下面选择路径:
$HOME/.config/Example/exaple.conf (Qt for Embedded Linux: $HOME/Settings/Example/exaple.conf)
$HOME/.config/Example.conf (Qt for Embedded Linux: $HOME/Settings/Example.conf)
for each directory <dir> in $XDG_CONFIG_DIRS: <dir>/Example/exaple.conf
for each directory <dir> in $XDG_CONFIG_DIRS: <dir>/Example.conf
*/
QSettings settings("Example", "example");
//没有设置group时默认位于General
settings.setValue("first","this is a example");
//写入值
settings.beginGroup("Date");
settings.setValue("date", QDate::currentDate().toString("yy/MM/dd"));
settings.setValue("number",100);
settings.endGroup();
settings.setValue("Date/number1",1); //也可以通过/分隔符来定位
//写入数组
struct Login {
QString userName;
QString password;
};
QList<Login> logins;
settings.beginWriteArray("logins");
for (int i = 0; i < 5; ++i) {
settings.setArrayIndex(i);
settings.setValue("userName", QString::number(i));
settings.setValue("password", QString::number(i));
}
settings.endArray();
//读取值
qDebug()<<"first = "<<settings.value("first").toString();
qDebug()<<"date = "<<settings.value("Date/date").toString();
qDebug()<<"number = "<<settings.value("Date/number").toInt();
int size = settings.beginReadArray("logins");
for (int i = 0; i < size; ++i) {
settings.setArrayIndex(i);
Login login;
login.userName = settings.value("userName").toString();
login.password = settings.value("password").toString();
logins.append(login);
qDebug()<<"username = "<<login.userName;
qDebug()<<"password = "<<login.password;
}
settings.endArray();
return a.exec();
}