Qt QSettings配置文件ini操作

QSettings类提供了持久的、平台无关的应用程序设置。用于保存和恢复应用程序的设置。它也支持自定义存储类型。

如果你需要的是一个非持久性的基于内存结构,可以考虑使用QMap<QString, QVariant>代替。

QSettings::Format有两种:
  QSettings::NativeFormat在windows平台可以读写windows注册表.
  QSettings::IniFormat可以读写ini格式的配置文件
  在Unix/X11平台下这两个Format效果是一样的:(
  1.读写注册表
  //注意的就是路径名必须是"/"而不是"//"等.否则不能读写,同时注意
  //Format为QSettings::NativeFormat
  QSettings*settings = newQSettings("HKEY_CURRENT_USER/",QSettings::NativeFormat);
  //写HKEY_CURRENT_USER/regedit/test,设置test值为red
  settings->setValue("regedit/test", "red");
  //读 HKEY_CURRENT_USER/regedit/test,默认值是ddd
  QString value = settings->value("regedit/test", "ddd").toString();
  删除设置对应的是settings->remove( const QString & key )
  2.读取ini配置文件
  ini文件格式如下:
  [Alpha]
  Beta/Beta=1, 3, 4
  Beta/Gamma=2
  Beta/Delta=1111
  Beta/Epsilon=5
  Iota=7
  Omicron=11
  QSettings*settings = newQSettings(fileName,QSettings::IniFormat);
  //写Beta/Beta
  settings->setValue("Alpha/Beta/Beta", 68);

#include <QCoreApplication>
#include <QFile>
#include <QSettings>
#include <QDebug>
#include <QDateTime>
struct userInfo
{
    QString userName;
    QString passWord;
};
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    //QSettings可以写入注册表,写入之前首先需要设置organizationname和applicationname,或者直接用构造函数,指定这两项
    QCoreApplication::setOrganizationName("Liuzc");
    QCoreApplication::setOrganizationDomain("shanghai");
    QCoreApplication::setApplicationName("myQSettings");
    QSettings settings;
    settings.setValue("server/ip","192.168.0.1");
    settings.setValue("server/port",10086);
    settings.setValue("server/isconnect", 0);
    settings.setValue("server/lastTime",QDateTime::currentDateTime());
    settings.setValue("client/ip","192.168.0.2");
    settings.setValue("client/port",5200);

    //读取
    QSettings settings2;
    QString strIP = settings2.value("server/ip","127.0.0.1").toString();
    qint32 nPort = settings2.value("server/port", 1000).toInt();
    bool bConnect = settings2.value("server/isconnect",true).toBool();
    QDateTime lastTime = settings2.value("server/lastTime", QDateTime::currentDateTime()).toDateTime();
    QString strClientIP = settings2.value("client/ip", "127.0.0.1").toString();
    settings2.remove("client/port");
    qint32 nClientPort = settings2.value("client/port", 1000).toInt();
    qDebug()<<strIP;
    qDebug()<<nPort;
    qDebug()<<bConnect;
    qDebug()<<lastTime;
    qDebug()<<strClientIP;
    qDebug()<<nClientPort;
    if(!settings2.contains("client/port"))
        qDebug()<<"no client/port";
    QStringList strList = settings2.allKeys();
    foreach (QString str, strList) {
        qDebug()<<str;
    }
    settings2.remove("client");
    strList = settings2.allKeys();
    foreach (QString str, strList) {
        qDebug()<<str;
    }

    //写入指定文件
    QString strPath = QCoreApplication::applicationDirPath()+"/config.ini";
    QSettings settings3(strPath,QSettings::IniFormat);
    settings3.setValue("dataConfig/user","zhangsan");
    settings3.setValue("dataConfig/age",30);
    settings3.beginGroup("dataConfig");
    settings3.setValue("height",140.5);
    settings3.endGroup();
    settings3.setValue("core/isvip",0);
    settings3.setValue("core/time",QDateTime::currentDateTime().toString());
    //读取指定文件
    int age = settings3.value("dataConfig/age",100).toInt();
    qDebug()<<"age:"<<age;
    //写入数组
    QList<userInfo> infoList;
    userInfo user;
    for(int i = 0;i<5;i++)
    {
        user.userName = QString("user%1").arg(i);
        user.passWord = "123456";
        infoList<<user;
    }
    settings3.beginWriteArray("userInfo1");
    for(int i =0;i<infoList.size();++i)
    {
        settings3.setArrayIndex(i);
        settings3.setValue("userName",infoList.at(i).userName);
        settings3.setValue("passWord",infoList.at(i).passWord);
    }
    settings3.endArray();

    QList<userInfo> infoList2;
    for(int i = 0;i<5;i++)
    {
        user.userName = QString("log%1").arg(i);
        user.passWord = "000000";
        infoList2<<user;
    }
    settings3.beginWriteArray("userInfo2");
    for(int i =0;i<infoList2.size();++i)
    {
        settings3.setArrayIndex(i);
        settings3.setValue("userName",infoList2.at(i).userName);
        settings3.setValue("passWord",infoList2.at(i).passWord);
    }
    settings3.endArray();

    //读取数组
    //获取所有组
    QStringList groups = settings3.childGroups();
    foreach (QString group, groups) {
       qDebug()<<"group:"<<group;
    }
    int nSize = settings3.beginReadArray("userInfo1");
    if(nSize >0)
    {
        for(int i=0;i<nSize;i++)
        {
            userInfo usr;
            settings3.setArrayIndex(i);
            usr.userName = settings3.value("userName").toString();
            usr.passWord = settings3.value("passWord").toString();
            qDebug()<<"userInfo1"<<i<<":"<<usr.userName<<","<<usr.passWord;
        }
    }
    settings3.endArray();
    return a.exec();
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值