zoukankan      html  css  js  c++  java
  • QSettings配置读写-win注册表操作-ini文件读写

    博客地址已更改,文章数量较多不便批量修改,若想访问源文请到 coologic博客 查阅,网址:www.coologic.cn

    如本文记录地址为 techieliang.com/A/B/C/ 请改为 www.coologic.cn/A/B/C/ 即可查阅

    版权声明:若无来源注明,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.  
    4. //"\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.  
    4. int main(int argc, char *argv[])
    5. {
    6. const QSettings::Format XmlFormat =
    7. QSettings::registerFormat("xml", readXmlFile, writeXmlFile);
    8.  
    9. QSettings settings(XmlFormat, QSettings::UserScope, "MySoft",
    10. "Star Runner");
    11.  
    12. ...
    13. }

    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)

    对应的默认路径如下:

    PlatformFormatScopePath
    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配置读写
  • 相关阅读:
    React Native 开发环境安装和配置使用报错: -bash: react-native: command not found
    ios开发不能不知的动态修复bug补丁第三方库JSPatch 使用学习:JSPatch导入、和使用、.js文件传输加解密
    ios开发视频播放后台下载功能实现 :1,ios播放视频 ,包含基于AVPlayer播放器,2,实现下载,iOS后台下载(多任务同时下载,单任务下载,下载进度,下载百分比,文件大小,下载状态)(真机调试功能正常)
    ios开发总结:Utils常用方法等收集,添加扩展类,工具类方法,拥有很多方便快捷功能(不断更新中。。。)
    alloc init初始化后对象依然还在父视图
    第二篇:_UICascadingTextStorage attributesAtIndex:effectiveRange:]: Range or index out of bounds
    Linux运维入门到高级全套常用要点
    高级Linux SA需要会做的事情
    squid源码安装下的conf文件默认值和提示
    将博客搬至CSDN
  • 原文地址:https://www.cnblogs.com/techiel/p/8030054.html
Copyright © 2011-2022 走看看