QT 配置檔案操作
阿新 • • 發佈:2018-12-14
利用QSettings設定配置檔案
#include <QSettings>
QSettings setting(QCoreApplication::applicationDirPath()+"/config.ini",QSettings::IniFormat);//判斷是否存在配置檔案,不存在則新建config.ini
//判斷是否存在這樣的鍵值對HostName,Port,UserName,Password
if(!setting.contains("HostName")||!setting.contains("Port")||!setting.contains ("UserName")||!setting.contains("Password"))
{
//不存在則建立初值,預設節General
setting.setValue("HostName","127.0.0.1");
setting.setValue("Port",3306);
setting.setValue("UserName","root");
setting.setValue("Password","123456");
}
QString HostName = setting.value ("HostName").toString();
int Port = setting.value("Port").toInt();
if(!setting.contains("com1/baud"))//com1為節,baud為鍵,520為值
setting.setValue("com1/baud",520); //不存在則建立則賦初值
qDebug()<<setting.value("com1/baud").toString();