在项目中经常需要保存配置在本地。Qt中QSettings实现了配置文件的读写。是一个很经常使用的类。
#include <QSettings>
#include <QtGui>
QString filePath = QDir::currentPath()+"/"+"Server.conf";
QSettings conf(filePath,QSettings::IniFormat);
QString oldip="127.0.0.1";
QString newip;
newip = conf.value("server/ip",oldip).toString();//get the value,如果配置文件不存在,取值为oldip; 127.0.0.1
conf.setValue("server/ip","192.168.1.1"); //save the value,写配置文件;
运行,在构建目录下生成Server.conf 文件。文件的内容为:
[server]
ip=192.168.1.1