Qt QSettings配置文件ini操作

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

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

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 
#include 
#include 
#include 
#include 
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()< infoList;
    userInfo user;
    for(int i = 0;i<5;i++)
    {
        user.userName = QString("user%1").arg(i);
        user.passWord = "123456";
        infoList< infoList2;
    for(int i = 0;i<5;i++)
    {
        user.userName = QString("log%1").arg(i);
        user.passWord = "000000";
        infoList2<0)
    {
        for(int i=0;i

 

你可能感兴趣的:(Qt)