背景:使用QSettings读写配置文件
示例及需要注意的问题:
①写配置文件
QSettings settings("Option.ini", QSettings::IniFormat);
settings.beginGroup("Config");//此句不可缺少
settings.setValue("Server", "我是服务器");
settings.setValue("Client", "我是客户端");
settings.endGroup();//此句不可缺少
②读配置文件
QSettings settings("Option.ini", QSettings::IniFormat);
// settings.beginGroup("Config");读取的时候不可加
qDebug()<<settings.value("Config/Server").toString();
qDebug()<<settings.value("Config/Client").toString();
// settings.endGroup();读取的时候不可加
总结:setValue()必须被包含在beginGroup()/endGroup()之间,beginGroup()/endGroup()之间不可包含value()
QString qstrConfigPath = qApp->applicationDirPath() + "/Config.ini";
QSettings* pConfigReader = new QSettings(qstrConfigPath, QSettings::IniFormat);
QString qstrCIP = pConfigReader->value("/url/cip").toString();
QString qstrCPort = pConfigReader->value("/url/cport").toString();
QString qstrSIP = pConfigReader->value("/url/sip").toString();
QString qstrSPort = pConfigReader->value("/url/sport").toString();
QString qstrNO = pConfigReader->value("/screen/no").toString();
delete pConfigReader;
注:applicationDirPath()为“获取当前执行程序路径”