1. 介紹
官方幫助文檔:QSettingshtml
一套完整的配置文件讀寫機制,多平臺支持,支持ini文件讀寫、win下注冊表讀寫等操做。同時支持當前用戶配置及當前系統配置兩個做用範圍。windows
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也同樣app
3若是設置爲ini類型會在」C:/Users/XXXXX/AppData/Roaming/」用戶範圍創建ini文件,」C:/ProgramData/」系統範圍函數
4能夠指定文件名和類型post
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. |
若是設置做用域爲用戶,則先檢查用戶,若是沒有再檢查系統範圍。若是設置爲系統則不會檢查用戶。this
2.3. 關於組織、程序名
程序具備全局惟一的組織及程序名,能夠直接使用。若是須要單獨創建則不須要spa
- QSettings settings("Moose Soft", "Facturo-Pro");//自定義
- //配置全局名稱並使用
- QCoreApplication::setOrganizationName("Moose Soft");
- QCoreApplication::setApplicationName("Facturo-Pro");
- QSettings settings;//此時能夠無參數構造
3. 配置文件讀寫
讀寫配置:setValue、valuecode
爲了數據的分類明確還提供了配置分組功能,須要使用beginGroup、endGroup 注意begin開始後面代碼表示在組內操做,若想訪問組外內容必須先endorm
讀寫時能夠不用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)
對應的默認路徑以下:
轉載請以連接形式標明本文標題和地址:
Techie亮博客 »
QSettings配置讀寫