2010-08-26 10:53:30
標籤:ini
檔案qt [
推送到技術圈]
原始出處、作者資訊和本宣告。否則將追究法律責任。
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=中文
需要#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())
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:",qsettings::iniformat,0);
configini->setvalue("/config/node1",ui->lineedit->text());
deleteconfigini;
qsettings *configini = new qsettings("d:",qsettings::iniformat,0); configini->setvalue("/config/node1",ui->lineedit->text()); delete configini;
4、用qsettings讀ini檔案:
qsettings *configini =newqsettings("d:",qsettings::iniformat,0);
ui->lineedit_2->settext(configini->value("/config/node1","20").tostring());
deleteconfigini;
qsettings *configini = new qsettings("d:",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部分講的很清楚
foreach (qstring group, mysetting->childgroups())
if (outstr == "工作語言")
if (outstr == "離線工作")
if (outstr == "召喚自描述方式")
if (outstr == "組播位址")
if (outstr == "接收埠號")
if (outstr == "傳送埠號")
if (outstr == "啟用本機位址")
if (outstr == "本機位址")
if (outstr == "裝置名稱")
if (outstr == "裝置位址")
if (outstr == "裝置ip")
}mysetting->endgroup();
本文出自 「今年我們二十三四」 部落格,請務必保留此出處
qt讀取中文ini配置檔案 qt讀取ini檔案
qsettings configini new qsettings qsettings ini configini insertsearchpath qsettings unix,home dawei 配置檔案目錄 configini writeentry qstring fromutf8 conf...
QT 讀取配置ini檔案
ini 檔案是initialization file的縮寫,即初始化檔案。格式 ini配置檔案由節 鍵 值組成。節 所有的引數都是以節 section 為單位結合在一起的。所有的section名稱都是獨佔一行,並且section名字都被方括號包圍著 和 在section宣告後的所有parameter...
QT學習筆記之讀取INI檔案
ini檔案由節 鍵 值組成。section 鍵 值 name value 乙個ini例項 student name xiaoming age 14我這裡是用的new方式,最後需要delete,在棧上定義乙個物件當然也可以 該建構函式的第乙個引數是要寫入的ini檔案名字,第二個引數標識 講設定儲存在i...