QSettings配置读写-win注册表操作-ini文件读写
本文標題:QSettings配置讀寫-win注冊表操作-ini文件讀寫???? 本文地址:http://techieliang.com/2017/12/674/ 文章目錄
- 1. 介紹
- 2. 創建配置文件
- ?2.1. 配置格式
- ?2.2. 作用域
- ?2.3. 關于組織、程序名
- 3. 配置文件讀寫
- 4. 范例
- ?4.1. win下SystemScope、IniFormat
- ?4.2. win下UserScope、IniFormat
- ?4.3. win下不設置IniFormat、UserScope
- ?4.4. win下不設置IniFormat、SystemScope
- ?4.5. win下InvalidFormat、SystemScope
- ?4.6. win下InvalidFormat、UserScope
- 5. AllKeys
- 6. 高級
- ?6.1. 自定義讀寫配置方法
- ?6.2. Win特例
- ?6.3. setPath函數-不同模式、范圍的默認路徑
1. 介紹
官方幫助文檔:QSettings
一套完整的配置文件讀寫機制,多平臺支持,支持ini文件讀寫、win下注冊表讀寫等操作。同時支持當前用戶配置及當前系統配置兩個作用范圍。
2. 創建配置文件
配置文件涉及到作用域(scope)、文件名(filename)、組織名(organization)、程序名(application)、配置格式(format)等,下面是可用的構造函數:
構造方式1在win下自動在注冊表讀寫,2也一樣
3如果設置為ini類型會在”C:/Users/XXXXX/AppData/Roaming/”用戶范圍建立ini文件,”C:/ProgramData/”系統范圍
4可以指定文件名和類型
2.1. 配置格式
| 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. |
| 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. |
| 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. |
| QSettings::IniFormat | 1 | Store the settings in INI files. |
| QSettings::InvalidFormat | 16 | Special value returned by registerFormat(). |
2.2. 作用域
| QSettings::UserScope | 0 | Store settings in a location specific to the current user (e.g., in the user’s home directory). |
| QSettings::SystemScope | 1 | Store settings in a global location, so that all users on the same machine access the same set of settings. |
如果設置作用域為用戶,則先檢查用戶,如果沒有再檢查系統范圍。如果設置為系統則不會檢查用戶。
2.3. 關于組織、程序名
程序具有全局唯一的組織及程序名,可以直接使用。如果需要單獨建立則不需要
3. 配置文件讀寫
讀寫配置:setValue、value
為了數據的分類明確還提供了配置分組功能,需要使用beginGroup、endGroup 注意begin開始后面代碼表示在組內操作,若想訪問組外內容必須先end
讀寫時可以不用group操作,通過以下方式也可表示組的概念:
同時支持數組beginReadArray、beginWriteArray、endArray 注意begin開始后面代碼表示在組內操作,若想訪問組外內容必須先end
除此以外還有remove刪除配置內容,注意此刪除是完全刪除此配置項,不是把當前配置內容制空
在讀數據之前可以使用contains判斷是否有當前key的配置項
還有具有范圍傷害的兩個方法:clear刪除所有配置項(注冊表需要符合組合和程序名,ini就是清空)、allKeys讀取當前group及下屬所有配置項key的名稱
4. 范例
上面的可能看不懂,下面遍歷一遍win下的配置格式和作用域
4.1. win下SystemScope、IniFormat
4.2. win下UserScope、IniFormat
4.3. win下不設置IniFormat、UserScope
4.4. win下不設置IniFormat、SystemScope
4.5. win下InvalidFormat、SystemScope
4.6. win下InvalidFormat、UserScope
5. AllKeys
6. 高級
6.1. 自定義讀寫配置方法
registerFormat(const QString &extension, ReadFunc readFunc, WriteFunc writeFunc, Qt::CaseSensitivity caseSensitivity = Qt::CaseSensitive)
此方法可以注冊自定義格式
6.2. Win特例
windows下可能一個key同時具有value和子項目,此時值需要通過Default或“.”來訪問
On Windows, it is possible for a key to have both a value and subkeys. Its default value is accessed by using “Default” or “.” in place of a subkey:
6.3. setPath函數-不同模式、范圍的默認路徑
如果開始設置的范圍、配置格式、文件路徑不對,也可以通過此函數修改,主要是按默認路徑
void QSettings::setPath(Format format, Scope scope, const QString &path)
對應的默認路徑如下:
| Windows | IniFormat | UserScope | FOLDERID_RoamingAppData |
| SystemScope | FOLDERID_ProgramData | ||
| Unix | NativeFormat, IniFormat | UserScope | $HOME/.config |
| SystemScope | /etc/xdg | ||
| Qt for Embedded Linux | NativeFormat, IniFormat | UserScope | $HOME/Settings |
| SystemScope | /etc/xdg | ||
| macOS and iOS | IniFormat | UserScope | $HOME/.config |
| SystemScope | /etc/xdg |
轉載于:https://www.cnblogs.com/techiel/p/8030054.html
總結
以上是生活随笔為你收集整理的QSettings配置读写-win注册表操作-ini文件读写的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python地址转经纬度_经纬度地址转换
- 下一篇: Telerik DevCraft cra