QSettings配置读写-win注册表操作-ini文件读写
本文標(biāo)題:QSettings配置讀寫-win注冊(cè)表操作-ini文件讀寫???? 本文地址:http://techieliang.com/2017/12/674/ 文章目錄
- 1. 介紹
- 2. 創(chuàng)建配置文件
- ?2.1. 配置格式
- ?2.2. 作用域
- ?2.3. 關(guān)于組織、程序名
- 3. 配置文件讀寫
- 4. 范例
- ?4.1. win下SystemScope、IniFormat
- ?4.2. win下UserScope、IniFormat
- ?4.3. win下不設(shè)置IniFormat、UserScope
- ?4.4. win下不設(shè)置IniFormat、SystemScope
- ?4.5. win下InvalidFormat、SystemScope
- ?4.6. win下InvalidFormat、UserScope
- 5. AllKeys
- 6. 高級(jí)
- ?6.1. 自定義讀寫配置方法
- ?6.2. Win特例
- ?6.3. setPath函數(shù)-不同模式、范圍的默認(rèn)路徑
1. 介紹
官方幫助文檔:QSettings
一套完整的配置文件讀寫機(jī)制,多平臺(tái)支持,支持ini文件讀寫、win下注冊(cè)表讀寫等操作。同時(shí)支持當(dāng)前用戶配置及當(dāng)前系統(tǒng)配置兩個(gè)作用范圍。
2. 創(chuàng)建配置文件
配置文件涉及到作用域(scope)、文件名(filename)、組織名(organization)、程序名(application)、配置格式(format)等,下面是可用的構(gòu)造函數(shù):
構(gòu)造方式1在win下自動(dòng)在注冊(cè)表讀寫,2也一樣
3如果設(shè)置為ini類型會(huì)在”C:/Users/XXXXX/AppData/Roaming/”用戶范圍建立ini文件,”C:/ProgramData/”系統(tǒng)范圍
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. |
如果設(shè)置作用域?yàn)橛脩?#xff0c;則先檢查用戶,如果沒有再檢查系統(tǒng)范圍。如果設(shè)置為系統(tǒng)則不會(huì)檢查用戶。
2.3. 關(guān)于組織、程序名
程序具有全局唯一的組織及程序名,可以直接使用。如果需要單獨(dú)建立則不需要
3. 配置文件讀寫
讀寫配置:setValue、value
為了數(shù)據(jù)的分類明確還提供了配置分組功能,需要使用beginGroup、endGroup 注意begin開始后面代碼表示在組內(nèi)操作,若想訪問組外內(nèi)容必須先end
讀寫時(shí)可以不用group操作,通過以下方式也可表示組的概念:
同時(shí)支持?jǐn)?shù)組beginReadArray、beginWriteArray、endArray 注意begin開始后面代碼表示在組內(nèi)操作,若想訪問組外內(nèi)容必須先end
除此以外還有remove刪除配置內(nèi)容,注意此刪除是完全刪除此配置項(xiàng),不是把當(dāng)前配置內(nèi)容制空
在讀數(shù)據(jù)之前可以使用contains判斷是否有當(dāng)前key的配置項(xiàng)
還有具有范圍傷害的兩個(gè)方法:clear刪除所有配置項(xiàng)(注冊(cè)表需要符合組合和程序名,ini就是清空)、allKeys讀取當(dāng)前group及下屬所有配置項(xiàng)key的名稱
4. 范例
上面的可能看不懂,下面遍歷一遍win下的配置格式和作用域
4.1. win下SystemScope、IniFormat
4.2. win下UserScope、IniFormat
4.3. win下不設(shè)置IniFormat、UserScope
4.4. win下不設(shè)置IniFormat、SystemScope
4.5. win下InvalidFormat、SystemScope
4.6. win下InvalidFormat、UserScope
5. AllKeys
6. 高級(jí)
6.1. 自定義讀寫配置方法
registerFormat(const QString &extension, ReadFunc readFunc, WriteFunc writeFunc, Qt::CaseSensitivity caseSensitivity = Qt::CaseSensitive)
此方法可以注冊(cè)自定義格式
6.2. Win特例
windows下可能一個(gè)key同時(shí)具有value和子項(xiàng)目,此時(shí)值需要通過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函數(shù)-不同模式、范圍的默認(rèn)路徑
如果開始設(shè)置的范圍、配置格式、文件路徑不對(duì),也可以通過此函數(shù)修改,主要是按默認(rèn)路徑
void QSettings::setPath(Format format, Scope scope, const QString &path)
對(duì)應(yīng)的默認(rèn)路徑如下:
| 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 |
轉(zhuǎn)載于:https://www.cnblogs.com/techiel/p/8030054.html
總結(jié)
以上是生活随笔為你收集整理的QSettings配置读写-win注册表操作-ini文件读写的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python地址转经纬度_经纬度地址转换
- 下一篇: Telerik DevCraft cra