版权声明:若无来源注明,Techie亮博客文章均为原创。 转载请以链接形式标明本文标题和地址:
本文标题:QSettings配置读写-win注册表操作-ini文件读写     本文地址:http://techieliang.com/2017/12/674/

1. 介绍

官方帮助文档:QSettings

一套完整的配置文件读写机制,多平台支持,支持ini文件读写、win下注册表读写等操作。同时支持当前用户配置及当前系统配置两个作用范围。

2. 创建配置文件

配置文件涉及到作用域(scope)、文件名(filename)、组织名(organization)、程序名(application)、配置格式(format)等,下面是可用的构造函数:

  1. QSettings(const QString &organization, const QString &application = QString(), QObject *parent = Q_NULLPTR) //1
  2. QSettings(Scope scope, const QString &organization, const QString &application = QString(), QObject *parent = Q_NULLPTR) //2
  3. QSettings(Format format, Scope scope, const QString &organization, const QString &application = QString(), QObject *parent = Q_NULLPTR) //3
  4. QSettings(const QString &fileName, Format format, QObject *parent = Q_NULLPTR) //3
  5. 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. 关于组织、程序名

程序具有全局唯一的组织及程序名,可以直接使用。如果需要单独建立则不需要

  1. QSettings settings("Moose Soft", "Facturo-Pro");//自定义
  2. //配置全局名称并使用
  3. QCoreApplication::setOrganizationName("Moose Soft");
  4. QCoreApplication::setApplicationName("Facturo-Pro");
  5. QSettings settings;//此时可以无参数构造

3. 配置文件读写

读写配置:setValue、value

为了数据的分类明确还提供了配置分组功能,需要使用beginGroupendGroup 注意begin开始后面代码表示在组内操作,若想访问组外内容必须先end

读写时可以不用group操作,通过以下方式也可表示组的概念:

  1. settings.setValue("mainwindow/size", win->size());
  2. settings.setValue("mainwindow/fullScreen", win->isFullScreen());
  3. settings.setValue("outputpanel/visible", panel->isVisible());
  4. //等效于:
  5. settings.beginGroup("mainwindow");
  6. settings.setValue("size", win->size());
  7. settings.setValue("fullScreen", win->isFullScreen());
  8. settings.endGroup();
  9. settings.beginGroup("outputpanel");
  10. settings.setValue("visible", panel->isVisible());
  11. settings.endGroup();

同时支持数组beginReadArraybeginWriteArrayendArray 注意begin开始后面代码表示在组内操作,若想访问组外内容必须先end

除此以外还有remove删除配置内容,注意此删除是完全删除此配置项,不是把当前配置内容制空

在读数据之前可以使用contains判断是否有当前key的配置项

还有具有范围伤害的两个方法:clear删除所有配置项(注册表需要符合组合和程序名,ini就是清空)、allKeys读取当前group及下属所有配置项key的名称

4. 范例

上面的可能看不懂,下面遍历一遍win下的配置格式和作用域

4.1. win下SystemScope、IniFormat

  1. #include <QCoreApplication>
  2. #include <QDebug>
  3. #include <QSettings>
  4. int main(int argc, char *argv[]) {
  5. QCoreApplication a(argc,argv);
  6. QSettings config(QSettings::IniFormat, QSettings::SystemScope,"TechieLiang", "testQSettings");
  7. qDebug()<< config.fileName();
  8. //写入配置文件
  9. config.beginGroup("config");
  10. config.setValue("user_name", "test");
  11. config.setValue("key", 123);
  12. config.endGroup();
  13. config.beginGroup("config");
  14. qDebug()<<config.value("user_name").toString()
  15. <<config.value("key").toInt();
  16. config.beginGroup("config");
  17. return 0;
  18. }
  19. //"C:/ProgramData/TechieLiang/testQSettings.ini"
  20. //"test" 123

4.2. win下UserScope、IniFormat

  1. #include <QCoreApplication>
  2. #include <QDebug>
  3. #include <QSettings>
  4. int main(int argc, char *argv[]) {
  5. QCoreApplication a(argc,argv);
  6. QSettings config(QSettings::IniFormat, QSettings::UserScope,"TechieLiang", "testQSettings");
  7. qDebug()<< config.fileName();
  8. return 0;
  9. }
  10. //"C:/Users/XXXX/AppData/Roaming/TechieLiang/testQSettings.ini"
  11. //XXXX用户名

4.3. win下不设置IniFormat、UserScope

  1. QSettings config(QSettings::UserScope,"TechieLiang", "testQSettings");
  2. qDebug()<< config.fileName();
  3. //"\\HKEY_CURRENT_USER\\Software\\TechieLiang\\testQSettings"

4.4. win下不设置IniFormat、SystemScope

  1. QSettings config(QSettings::SystemScope,"TechieLiang", "testQSettings");
  2. //"\\HKEY_LOCAL_MACHINE\\Software\\TechieLiang\\testQSettings"

4.5. win下InvalidFormat、SystemScope

  1. QSettings config(QSettings::InvalidFormat,QSettings::SystemScope,"TechieLiang", "testQSettings");
  2. //"C:/ProgramData/TechieLiang/testQSettings.ini"

4.6. win下InvalidFormat、UserScope

  1. QSettings config(QSettings::InvalidFormat,QSettings::UserScope,"TechieLiang", "testQSettings");
  2. //"C:/Users/XXXX/AppData/Roaming/TechieLiang/testQSettings.ini"
  3. //XXXX用户名

5. AllKeys

  1. QSettings settings;
  2. settings.setValue("fridge/color", QColor(Qt::white));
  3. settings.setValue("fridge/size", QSize(32, 96));
  4. settings.setValue("sofa", true);
  5. settings.setValue("tv", false);
  6. QStringList keys = settings.allKeys();
  7. // keys: ["fridge/color", "fridge/size", "sofa", "tv"]
  8. settings.beginGroup("fridge");
  9. keys = settings.allKeys();
  10. // keys: ["color", "size"]

6. 高级

6.1. 自定义读写配置方法

registerFormat(const QString &extension, ReadFunc readFunc, WriteFunc writeFunc, Qt::CaseSensitivity caseSensitivity = Qt::CaseSensitive)

此方法可以注册自定义格式

  1. bool readXmlFile(QIODevice &device, QSettings::SettingsMap &map);
  2. bool writeXmlFile(QIODevice &device, const QSettings::SettingsMap &map);
  3. int main(int argc, char *argv[])
  4. {
  5. const QSettings::Format XmlFormat =
  6. QSettings::registerFormat("xml", readXmlFile, writeXmlFile);
  7. QSettings settings(XmlFormat, QSettings::UserScope, "MySoft",
  8. "Star Runner");
  9. ...
  10. }

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:

  1. settings.setValue("HKEY_CURRENT_USER\\MySoft\\Star Runner\\Galaxy", "Milkyway");
  2. settings.setValue("HKEY_CURRENT_USER\\MySoft\\Star Runner\\Galaxy\\Sun", "OurStar");
  3. 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
转载请以链接形式标明本文标题和地址:Techie亮博客 » QSettings配置读写

QSettings配置读写-win注册表操作-ini文件读写的更多相关文章

  1. delphi 注册表操作(读取、添加、删除、修改)完全手册

    DELPHI VS PASCAL(87)  32位Delphi程序中可利用TRegistry对象来存取注册表文件中的信息. 一.创建和释放TRegistry对象 1.创建TRegistry对象.为了操 ...

  2. Delphi的注册表操作

    转帖:Delphi的注册表操作 2009-12-21 11:12:52 分类: Delphi的注册表操作 32位Delphi程序中可利用TRegistry对象来存取注册表文件中的信息.     一.创 ...

  3. 【读书笔记】C#高级编程 第二十四章 文件和注册表操作

    (一)文件和注册表 对于文件系统操作,相关的类几乎都在System.IO名称空间中,而注册表操作由System.Win32名称空间中的类来处理. (二)管理文件系统 System.MarshalByR ...

  4. MFC学习 文件操作注册表操作

    c读写文件 void CFileView::OnRead() { FILE *pFile = fopen("1.txt", "r"); /*char ch[10 ...

  5. 《天书夜读:从汇编语言到windows内核编程》八 文件操作与注册表操作

    1)Windows运用程序的文件与注册表操作进入R0层之后,都有对应的内核函数实现.在windows内核中,无论打开的是文件.注册表或者设备,都需要使用InitializeObjectAttribut ...

  6. 注册表操作 Microsoft.Win32.Registry与RegistryKey类

    一.注册表操作简介 Registry 类,RegistryKey 类提供了操作注册表的接口 RegistryValueKind:用于指定操作注册表的数据类型 一.注册表巢 在注册表中,最上面的节点是注 ...

  7. CRegKey 注册表操作

    CRegKey 注册表操作 标签: accessnulluserpathbyteie 2011-11-03 13:55 3477人阅读 评论(0) 收藏 举报  分类: win32(7)  1.简介 ...

  8. C# 我的注册表操作类

    using System; using System.Collections.Generic; using System.Text; using Microsoft.Win32; using Syst ...

  9. C#注册表操作类--完整优化版

    using System; using System.Collections.Generic; using System.Text; using Microsoft.Win32; namespace ...

随机推荐

  1. idea创建Scala入门HelloWorld

    Scala开发环境的搭建 首先到Scala官网下载Scala网址为 https://www.scala-lang.org/download/ 找到下图所示位置:选择相对应的版本的Scala进行下载,这 ...

  2. express添加权限拦截

    express通过中间件的方式添加权限拦截 示例代码如下 app.get('/logout', checkLogin); app.get('/logout', function(req, res) { ...

  3. gulp 输出到同一目录

    gulp.task('jsx', function () { var src='app/script/**/*.jsx'; // src='app/script/components/selloff/ ...

  4. 《阿里巴巴Java开发手册》代码格式部分应用——idea中checkstyle的使用教程

    <阿里巴巴Java开发手册>代码格式部分应用--idea中checkstyle的使用教程 1.<阿里巴巴Java开发手册> 这是阿里巴巴工程师送给各位软件工程师的宝典,就像开车 ...

  5. 20155227 2016-2017-2 《Java程序设计》第一周学习总结

    20155227 2016-2017-2 <Java程序设计>第一周学习总结 教材学习内容总结 浏览教材,根据自己的理解每章提出一个问题 Java三个平台的区别. JDK.JRE.JVM区 ...

  6. 20145226夏艺华 《Java程序设计》预备作业3

    安装虚拟机 上学期开学的时候就安装了Linux虚拟机,由于我的是Mac OS,所以和windows下的安装有所不同. 我使用的是VirtualBoxVM虚拟机,稳定性还不错,需要的同学可以从https ...

  7. 实现Django ORM admin view中model字段choices取值自动更新的一种方法

    有两个表,一个是记录网站信息的site表,结构如下: CREATE TABLE `site` ( `id` ) unsigned NOT NULL AUTO_INCREMENT, `name` ) N ...

  8. Spring Boot:Caused by: org.apache.ibatis.binding.BindingException: Parameter 'deptId' not found.

    1. 错误信息描述 在使用Spring Boot + Mybaits从前台向后台提交数据时,控制台报出该错误信息 2. 报错原因 在dao接口中,该方法拥有两个参数,Mybaits无法区分这两个参数 ...

  9. string[]转换为int[]

    今天碰到一个问题,要把string[]转换为int[],但是又不想使用循环转换,找了好久最后找到了这种方法,特此记录下. string[] input = { "1", " ...

  10. 第六篇 native 版本的Postman如何通过代理服务器录制Web及手机APP请求

    第四篇主要介绍了chrome app版本的postman如何安装及如何录制Web脚本,比较简单. 但是chrome app 版本和native 版本相比,对应chrome app 版本官方已经放弃支持 ...