日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

QSettings配置读写-win注册表操作-ini文件读写

發(fā)布時(shí)間:2024/1/1 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 QSettings配置读写-win注册表操作-ini文件读写 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
版權(quán)聲明:若無來源注明,Techie亮博客文章均為原創(chuàng)。 轉(zhuǎn)載請(qǐng)以鏈接形式標(biāo)明本文標(biāo)題和地址:
本文標(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ù):

  • 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)
  • 構(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. 配置格式

    ConstantValueDescription
    QSettings::NativeFormat0Store 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::Registry32Format2Windows 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::Registry64Format3Windows 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::IniFormat1Store the settings in INI files.
    QSettings::InvalidFormat16Special value returned by registerFormat().

    2.2. 作用域

    QSettings::UserScope0Store settings in a location specific to the current user (e.g., in the user’s home directory).
    QSettings::SystemScope1Store 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ú)建立則不需要

  • QSettings settings("Moose Soft", "Facturo-Pro");//自定義
  • //配置全局名稱并使用
  • QCoreApplication::setOrganizationName("Moose Soft");
  • QCoreApplication::setApplicationName("Facturo-Pro");
  • QSettings settings;//此時(shí)可以無參數(shù)構(gòu)造
  • 3. 配置文件讀寫

    讀寫配置:setValue、value

    為了數(shù)據(jù)的分類明確還提供了配置分組功能,需要使用beginGroupendGroup 注意begin開始后面代碼表示在組內(nèi)操作,若想訪問組外內(nèi)容必須先end

    讀寫時(shí)可以不用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();
  • 同時(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

  • #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下不設(shè)置IniFormat、UserScope

  • QSettings config(QSettings::UserScope,"TechieLiang", "testQSettings");
  • qDebug()<< config.fileName();
  • ?
  • //"\\HKEY_CURRENT_USER\\Software\\TechieLiang\\testQSettings"
  • 4.4. win下不設(shè)置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. 高級(jí)

    6.1. 自定義讀寫配置方法

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

    此方法可以注冊(cè)自定義格式

  • 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下可能一個(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:

  • 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函數(shù)-不同模式、范圍的默認(rèn)路徑

    如果開始設(shè)置的范圍、配置格式、文件路徑不對(duì),也可以通過此函數(shù)修改,主要是按默認(rèn)路徑

    void QSettings::setPath(Format format, Scope scope, const QString &path)

    對(duì)應(yīng)的默認(rèn)路徑如下:

    PlatformFormatScopePath
    WindowsIniFormatUserScopeFOLDERID_RoamingAppData
    SystemScopeFOLDERID_ProgramData
    UnixNativeFormat, IniFormatUserScope$HOME/.config
    SystemScope/etc/xdg
    Qt for Embedded LinuxNativeFormat, IniFormatUserScope$HOME/Settings
    SystemScope/etc/xdg
    macOS and iOSIniFormatUserScope$HOME/.config
    SystemScope/etc/xdg
    轉(zhuǎn)載請(qǐng)以鏈接形式標(biāo)明本文標(biāo)題和地址:Techie亮博客 ? QSettings配置讀寫

    轉(zhuǎn)載于:https://www.cnblogs.com/techiel/p/8030054.html

    總結(jié)

    以上是生活随笔為你收集整理的QSettings配置读写-win注册表操作-ini文件读写的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。