先介紹一下ini文件:
.ini 文件是一種初始化文件,是windows的系統配置文件所採用的存儲格式,統管windows的各項配置,一般用戶就用windows提供的各項圖形化管理界面就可實現相同的配置了
.ini文件使用中發現的一些優點:
1、可以多個程序同時操作一個ini文件,不用擔心有問題怎麼滴
2、有根結點和關鍵字,類似於XML,方便查找
ini文件讀取方式
QSettings settings(strFilePath, QSettings::IniFormat); // strFilePath 文件所在路徑,比如"../***/****/****.ini";
settings.setIniCodec("UTF8"); // 設置編碼方式 --->解析ini配置文件中的文字問題
settings.beginGroup("DATAS"); // 從根結點DATAS開始查找關鍵字
QString strValue1 = settings.value("key").toString(); // 從關鍵字key中取數據
// QString strValue1 = settings.value("DATAS/key", "").toString();
settings.endGroup();
ini文件寫入方式
QSettings settings(strFilePath, QSettings::IniFormat); // strFilePath 文件所在路徑,比如"../***/****/****.ini";
settings.beginGroup("DATAS"); // 從根結點DATAS開始查找關鍵字
settings.setvalue("key", "value") ; // 給關鍵字key中寫入數據,數據內容爲value
settings.endGroup();
// settings.setvalue("DATAS/key", "value") ; //這樣可以不用組直接寫入,類似於上面分組
ini在使用中發現的一個問題,如果是在一個函數中,那麼只有這個函數執行完,這個時候,纔會往ini中寫入內容,和TXT有一定的區別,TXT只要關閉你打開的文件,這個時候內容就已經保存到文件中了,或者直接通過flush
強制寫入也可以,所以在和其他程序通過ini文件配合時需要注意
QSettings獲取數據,沒有數據獲取默認值
QSettings settings(strFilePath, QSettings::IniFormat); // strFilePath 文件所在路徑,比如"../***/****/****.ini";
settings.beginGroup("DATAS"); // 從根結點DATAS開始查找關鍵字
QString strValue1 = settings.value("key", "").toString(); // 從關鍵字key中取數據,第二個參數代表默認值
settings.endGroup();
// QString strValue1 = settings.value("DATAS/key", "").toString(); // 類似分組
QSettings 寫入註冊表
// strPath 寫入位置
QSettings reg(strPath, QSettings::NativeFormat);// strItemName 代表key值,strItemData代表 value值
reg.setValue(strItemName, strItemData);
// 示例
QSettings *reg = new QSettings("HKEY_CURRENT_USER//Software//yinhaifan", QSettings::NativeFormat);
reg->setValue("registered", true);
QSettings 讀取註冊表(和普通讀取方法一樣)
QSettings reg(strPath, QSettings::NativeFormat);
QString strValue = reg.value(strItemName).toString();
QSettings 刪除註冊表(和普通讀取方法一樣)
QSettings reg(strPath, QSettings::NativeFormat);
reg.remove(“Name”); // 刪除key值爲Name的介質對(也可以刪除整個組)
QStringList keys = settings.allKeys(); // 查看剩餘key值
// settings.clear(); 刪除所有介質對
QSettings格式總結
Constant | Value | Description | |
---|---|---|---|
QSettings::NativeFormat | 0 | Store the settings using the most appropriate storage format for the platform. On Windows, this means the system registry; on macOS and iOS, this means the CFPreferences API; on Unix, this means textual configuration files in INI format. | 使用最適合平臺的存儲格式存儲設置。在Windows上,這意味着系統註冊表;在macOS和iOS上,這意味着CFPreferences API;在Unix上,這意味着INI格式的文本配置文件。 |
QSettings::Registry32Format | 2 | Windows only: Explicitly access the 32-bit system registry from a 64-bit application running on 64-bit Windows. On 32-bit Windows or from a 32-bit application on 64-bit Windows, this works the same as specifying NativeFormat. This enum value was added in Qt 5.7. | 僅適用於Windows:從運行在64位Windows上的64位應用程序顯式地訪問32位系統註冊表。在32位Windows或64位Windows上的32位應用程序中,這與指定NativeFormat的工作原理相同。enum值在Qt 5.7中添加。 |
QSettings::Registry64Format | 3 | Windows only: Explicitly access the 64-bit system registry from a 32-bit application running on 64-bit Windows. On 32-bit Windows or from a 64-bit application on 64-bit Windows, this works the same as specifying NativeFormat. This enum value was added in Qt 5.7. | 僅適用於Windows:從運行在64位Windows上的32位應用程序顯式地訪問64位系統註冊表。在32位Windows或64位Windows上的64位應用程序中,這與指定NativeFormat的工作原理相同。enum值在Qt 5.7中添加。 |
QSettings::IniFormat | 1 | Store the settings in INI files. | 將設置存儲在INI文件中。 |
QSettings::InvalidFormat | 16 | Special value returned by registerFormat(). | registerFormat()返回的特殊值。 |
QSettings::InvalidFormat
註冊自定義存儲格式。如果成功,則返回一個特殊的格式值,然後可以將該值傳遞給QSettings構造函數。如果失敗,則返回InvalidFormat。