qsettings *configini = new qsettings(qsettings::ini);
configini->insertsearchpath(qsettings::unix, "/home/dawei");.//配置檔案目錄
configini->writeentry(qstring::fromutf8("/config./opt/引數1"), qstring::fromutf8("中文"));
string aaa = configini->readentry(qstring::fromutf8("/config./opt/引數1");
delete configini;
會在/home/dawei目錄下生成config.rc配置檔案內容為
[opt]
引數1=中文
qstring db_hostname = settings.value("hostname").tostring();
qstring db_database = settings.value("database").tostring();
qstring db_username = settings.value("username").tostring();
qstring db_password = settings.value("password").tostring();
qsqldatabase db = qsqldatabase::adddatabase("qmysql");
db.sethostname(db_hostname);
db.setdatabasename(db_database);
db.setusername(db_username);
db.setpassword(db_password);
需要#include
只舉幾個簡單的例子:
1、用qsettings寫登錄檔
qsettings *reg =newqsettings("hkey_current_user\\software\\yinhaifan",
qsettings::nativeformat);
reg->setvalue("registered",true);
deletereg;
qsettings *reg = new qsettings("hkey_current_user\\software\\yinhaifan", qsettings::nativeformat); reg->setvalue("registered",true); delete reg;
當然reg->setvalue還可以寫入字串,整型之類的資料。
比如reg->setvalue("registered","yes");
2、用qstring讀登錄檔
qsettings *reg =newqsettings("hkey_current_user\\software\\yinhaifan",
qsettings::nativeformat);
if(reg->value("registered",false).tobool())
qmessagebox::information(null,"information","registered");
deletereg;
qsettings *reg = new qsettings("hkey_current_user\\software\\yinhaifan", qsettings::nativeformat); if (reg->value("registered",false).tobool()) delete reg;
還可以toint(),tostring()等。
3、用qsettings寫ini檔案:
qsettings *configini =newqsettings("d:\\a.ini",qsettings::iniformat,0);
configini->setvalue("/config/node1",ui->lineedit->text());
deleteconfigini;
qsettings *configini = new qsettings("d:\\a.ini",qsettings::iniformat,0); configini->setvalue("/config/node1",ui->lineedit->text()); delete configini;
4、用qsettings讀ini檔案:
qsettings *configini =newqsettings("d:\\a.ini",qsettings::iniformat,0);
ui->lineedit_2->settext(configini->value("/config/node1","20").tostring());
deleteconfigini;
qsettings *configini = new qsettings("d:\\a.ini",qsettings::iniformat,0); ui->lineedit_2->settext(configini->value("/config/node1","20").tostring()); delete configini;
這幾段**都是在qt creator裡面寫的,所以用ui->lineedit*->text()和ui->lineedit*->settext()做輸入和輸出了。
在文件中的qsettings class reference部分講的很清楚
m_ndevno = ndevno;
qstring strtemp;
qsettings *mysetting = new qsettings(xmlpath,qsettings::iniformat);
foreach (qstring group, mysetting->childgroups())
mysetting->begingroup(group);
foreach (qstring key, mysetting->childkeys())
qtextcodec *codec=qtextcodec::codecforname("gbk");
qstring outstr = codec->tounicode(key.tolatin1());
if (outstr == "自描述語言")
if ( mysetting->value(key,"0").toint() == 1)
ui.selfdsplgu->setcurrentindex(1);
else
ui.selfdsplgu->setcurrentindex(0);
if (outstr == "工作語言")
if ( mysetting->value(key,"0").toint() == 1)
ui.worklgu->setcurrentindex(1);
else
ui.worklgu->setcurrentindex(0);
if (outstr == "離線工作")
if ( mysetting->value(key,"0").toint() == 1)
ui.workmode->setcurrentindex(1);
else
ui.workmode->setcurrentindex(0);
if (outstr == "召喚自描述方式")
if ( mysetting->value(key,"0").toint() == 1)
ui.selfdspsource->setcurrentindex(1);
else
ui.selfdspsource->setcurrentindex(0);
if (outstr == "組播位址")
strtemp = mysetting->value(key,"236.8.8.8").tostring();
ui.grpaddress->settext(strtemp);
if (outstr == "接收埠號")
strtemp = mysetting->value("接收埠號","2000").tostring();
ui.recport->settext(strtemp);
if (outstr == "傳送埠號")
strtemp = mysetting->value("傳送埠號","2001").tostring();
ui.targetport->settext(strtemp);
if (outstr == "啟用本機位址")
strtemp = mysetting->value(key,"0").tostring();
if (outstr == "本機位址")
strtemp = mysetting->value(key,"0").tostring();
ui.localip->settext(strtemp);
if (outstr == "裝置名稱")
strtemp = mysetting->value(key,"0").tostring();
ui.eqpname->settext(strtemp);
if (outstr == "裝置位址")
strtemp = mysetting->value(key,"0").tostring();
ui.eqpaddress->settext(strtemp);
if (outstr == "裝置ip")
strtemp = mysetting->value(key,"0").tostring();
ui.targetip->settext(strtemp);
mysetting->endgroup();
qt怎麼讀取配置項 QT讀寫配置檔案 ini
ini ini檔案格式是某些平台或軟體上的配置檔案的非正式標準,以節 section 和鍵 key 構成,常用於微軟windows作業系統中。ini檔案是initialization file的縮寫,即初始化檔案。除了windows現在很多其他作業系統下面的應用軟體也有.ini檔案,用來配置應用軟體...
QT 讀取配置ini檔案
ini 檔案是initialization file的縮寫,即初始化檔案。格式 ini配置檔案由節 鍵 值組成。節 所有的引數都是以節 section 為單位結合在一起的。所有的section名稱都是獨佔一行,並且section名字都被方括號包圍著 和 在section宣告後的所有parameter...
qt 讀寫配置檔案 ini
ini 檔案是 initialization file 的縮寫,即初始化檔案。除了windows 現在很多其他作業系統下面的應用軟體也有.ini 檔案,用來配置應用軟體以實現不同使用者的要求。一般不用直接編輯這些 ini 檔案,應用程式的圖形介面即可操作以實現相同的功能。它可以用來存放軟體資訊,登錄...