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)等,下面是可用的构造函数:
- QSettings(const QString &organization, const QString &application = QString(), QObject *parent = Q_NULLPTR) //1
- QSettings(Scope scope, const QString &organization, const QString &application = QString(), QObject *parent = Q_NULLPTR) //2
- QSettings(Format format, Scope scope, const QString &organization, const QString &application = QString(), QObject *parent = Q_NULLPTR) //3
- QSettings(const QString &fileName, Format format, QObject *parent = Q_NULLPTR) //3
- QSettings(QObject *parent = Q_NULLPTR)
构造方式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. 关于组织、程序名
程序具有全局唯一的组织及程序名,可以直接使用。如果需要单独建立则不需要
- QSettings settings("Moose Soft", "Facturo-Pro");//自定义
- //配置全局名称并使用
- QCoreApplication::setOrganizationName("Moose Soft");
- QCoreApplication::setApplicationName("Facturo-Pro");
- QSettings settings;//此时可以无参数构造
3. 配置文件读写
读写配置:setValue、value
为了数据的分类明确还提供了配置分组功能,需要使用beginGroup、endGroup 注意begin开始后面代码表示在组内操作,若想访问组外内容必须先end
读写时可以不用group操作,通过以下方式也可表示组的概念:
- settings.setValue("mainwindow/size", win->size());
- settings.setValue("mainwindow/fullScreen", win->isFullScreen());
- settings.setValue("outputpanel/visible", panel->isVisible());
- //等效于:
- settings.beginGroup("mainwindow");
- settings.setValue("size", win->size());
- settings.setValue("fullScreen", win->isFullScreen());
- settings.endGroup();
- settings.beginGroup("outputpanel");
- settings.setValue("visible", panel->isVisible());
- settings.endGroup();
同时支持数组beginReadArray、beginWriteArray、endArray 注意begin开始后面代码表示在组内操作,若想访问组外内容必须先end
除此以外还有remove删除配置内容,注意此删除是完全删除此配置项,不是把当前配置内容制空
在读数据之前可以使用contains判断是否有当前key的配置项
还有具有范围伤害的两个方法:clear删除所有配置项(注册表需要符合组合和程序名,ini就是清空)、allKeys读取当前group及下属所有配置项key的名称
4. 范例
上面的可能看不懂,下面遍历一遍win下的配置格式和作用域
4.1. win下SystemScope、IniFormat
- #include <QCoreApplication>
- #include <QDebug>
- #include <QSettings>
- int main(int argc, char *argv[]) {
- QCoreApplication a(argc,argv);
- QSettings config(QSettings::IniFormat, QSettings::SystemScope,"TechieLiang", "testQSettings");
- qDebug()<< config.fileName();
- //写入配置文件
- config.beginGroup("config");
- config.setValue("user_name", "test");
- config.setValue("key", 123);
- config.endGroup();
- config.beginGroup("config");
- qDebug()<<config.value("user_name").toString()
- <<config.value("key").toInt();
- config.beginGroup("config");
- return 0;
- }
- //"C:/ProgramData/TechieLiang/testQSettings.ini"
- //"test" 123
4.2. win下UserScope、IniFormat
- #include <QCoreApplication>
- #include <QDebug>
- #include <QSettings>
- int main(int argc, char *argv[]) {
- QCoreApplication a(argc,argv);
- QSettings config(QSettings::IniFormat, QSettings::UserScope,"TechieLiang", "testQSettings");
- qDebug()<< config.fileName();
- return 0;
- }
- //"C:/Users/XXXX/AppData/Roaming/TechieLiang/testQSettings.ini"
- //XXXX用户名
4.3. win下不设置IniFormat、UserScope
- QSettings config(QSettings::UserScope,"TechieLiang", "testQSettings");
- qDebug()<< config.fileName();
- //"\\HKEY_CURRENT_USER\\Software\\TechieLiang\\testQSettings"
4.4. win下不设置IniFormat、SystemScope
- QSettings config(QSettings::SystemScope,"TechieLiang", "testQSettings");
- //"\\HKEY_LOCAL_MACHINE\\Software\\TechieLiang\\testQSettings"
4.5. win下InvalidFormat、SystemScope
- QSettings config(QSettings::InvalidFormat,QSettings::SystemScope,"TechieLiang", "testQSettings");
- //"C:/ProgramData/TechieLiang/testQSettings.ini"
4.6. win下InvalidFormat、UserScope
- QSettings config(QSettings::InvalidFormat,QSettings::UserScope,"TechieLiang", "testQSettings");
- //"C:/Users/XXXX/AppData/Roaming/TechieLiang/testQSettings.ini"
- //XXXX用户名
5. AllKeys
- QSettings settings;
- settings.setValue("fridge/color", QColor(Qt::white));
- settings.setValue("fridge/size", QSize(32, 96));
- settings.setValue("sofa", true);
- settings.setValue("tv", false);
- QStringList keys = settings.allKeys();
- // keys: ["fridge/color", "fridge/size", "sofa", "tv"]
- settings.beginGroup("fridge");
- keys = settings.allKeys();
- // keys: ["color", "size"]
6. 高级
6.1. 自定义读写配置方法
registerFormat(const QString &extension, ReadFunc readFunc, WriteFunc writeFunc, Qt::CaseSensitivity caseSensitivity = Qt::CaseSensitive)
此方法可以注册自定义格式
- bool readXmlFile(QIODevice &device, QSettings::SettingsMap &map);
- bool writeXmlFile(QIODevice &device, const QSettings::SettingsMap &map);
- int main(int argc, char *argv[])
- {
- const QSettings::Format XmlFormat =
- QSettings::registerFormat("xml", readXmlFile, writeXmlFile);
- QSettings settings(XmlFormat, QSettings::UserScope, "MySoft",
- "Star Runner");
- ...
- }
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:
- settings.setValue("HKEY_CURRENT_USER\\MySoft\\Star Runner\\Galaxy", "Milkyway");
- settings.setValue("HKEY_CURRENT_USER\\MySoft\\Star Runner\\Galaxy\\Sun", "OurStar");
- settings.value("HKEY_CURRENT_USER\\MySoft\\Star Runner\\Galaxy\\Default"); // returns "Milkyway"
6.3. setPath函数-不同模式、范围的默认路径
如果开始设置的范围、配置格式、文件路径不对,也可以通过此函数修改,主要是按默认路径
void QSettings::setPath(Format format, Scope scope, const QString &path)
对应的默认路径如下:
| Platform | Format | Scope | 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 |
QSettings配置读写-win注册表操作-ini文件读写的更多相关文章
- delphi 注册表操作(读取、添加、删除、修改)完全手册
DELPHI VS PASCAL(87) 32位Delphi程序中可利用TRegistry对象来存取注册表文件中的信息. 一.创建和释放TRegistry对象 1.创建TRegistry对象.为了操 ...
- Delphi的注册表操作
转帖:Delphi的注册表操作 2009-12-21 11:12:52 分类: Delphi的注册表操作 32位Delphi程序中可利用TRegistry对象来存取注册表文件中的信息. 一.创 ...
- 【读书笔记】C#高级编程 第二十四章 文件和注册表操作
(一)文件和注册表 对于文件系统操作,相关的类几乎都在System.IO名称空间中,而注册表操作由System.Win32名称空间中的类来处理. (二)管理文件系统 System.MarshalByR ...
- MFC学习 文件操作注册表操作
c读写文件 void CFileView::OnRead() { FILE *pFile = fopen("1.txt", "r"); /*char ch[10 ...
- 《天书夜读:从汇编语言到windows内核编程》八 文件操作与注册表操作
1)Windows运用程序的文件与注册表操作进入R0层之后,都有对应的内核函数实现.在windows内核中,无论打开的是文件.注册表或者设备,都需要使用InitializeObjectAttribut ...
- 注册表操作 Microsoft.Win32.Registry与RegistryKey类
一.注册表操作简介 Registry 类,RegistryKey 类提供了操作注册表的接口 RegistryValueKind:用于指定操作注册表的数据类型 一.注册表巢 在注册表中,最上面的节点是注 ...
- CRegKey 注册表操作
CRegKey 注册表操作 标签: accessnulluserpathbyteie 2011-11-03 13:55 3477人阅读 评论(0) 收藏 举报 分类: win32(7) 1.简介 ...
- C# 我的注册表操作类
using System; using System.Collections.Generic; using System.Text; using Microsoft.Win32; using Syst ...
- C#注册表操作类--完整优化版
using System; using System.Collections.Generic; using System.Text; using Microsoft.Win32; namespace ...
随机推荐
- idea创建Scala入门HelloWorld
Scala开发环境的搭建 首先到Scala官网下载Scala网址为 https://www.scala-lang.org/download/ 找到下图所示位置:选择相对应的版本的Scala进行下载,这 ...
- express添加权限拦截
express通过中间件的方式添加权限拦截 示例代码如下 app.get('/logout', checkLogin); app.get('/logout', function(req, res) { ...
- gulp 输出到同一目录
gulp.task('jsx', function () { var src='app/script/**/*.jsx'; // src='app/script/components/selloff/ ...
- 《阿里巴巴Java开发手册》代码格式部分应用——idea中checkstyle的使用教程
<阿里巴巴Java开发手册>代码格式部分应用--idea中checkstyle的使用教程 1.<阿里巴巴Java开发手册> 这是阿里巴巴工程师送给各位软件工程师的宝典,就像开车 ...
- 20155227 2016-2017-2 《Java程序设计》第一周学习总结
20155227 2016-2017-2 <Java程序设计>第一周学习总结 教材学习内容总结 浏览教材,根据自己的理解每章提出一个问题 Java三个平台的区别. JDK.JRE.JVM区 ...
- 20145226夏艺华 《Java程序设计》预备作业3
安装虚拟机 上学期开学的时候就安装了Linux虚拟机,由于我的是Mac OS,所以和windows下的安装有所不同. 我使用的是VirtualBoxVM虚拟机,稳定性还不错,需要的同学可以从https ...
- 实现Django ORM admin view中model字段choices取值自动更新的一种方法
有两个表,一个是记录网站信息的site表,结构如下: CREATE TABLE `site` ( `id` ) unsigned NOT NULL AUTO_INCREMENT, `name` ) N ...
- Spring Boot:Caused by: org.apache.ibatis.binding.BindingException: Parameter 'deptId' not found.
1. 错误信息描述 在使用Spring Boot + Mybaits从前台向后台提交数据时,控制台报出该错误信息 2. 报错原因 在dao接口中,该方法拥有两个参数,Mybaits无法区分这两个参数 ...
- string[]转换为int[]
今天碰到一个问题,要把string[]转换为int[],但是又不想使用循环转换,找了好久最后找到了这种方法,特此记录下. string[] input = { "1", " ...
- 第六篇 native 版本的Postman如何通过代理服务器录制Web及手机APP请求
第四篇主要介绍了chrome app版本的postman如何安装及如何录制Web脚本,比较简单. 但是chrome app 版本和native 版本相比,对应chrome app 版本官方已经放弃支持 ...