QSettings 类是 Qt 框架中的一个重要类,用于存储和访问应用程序的设置和配置。它提供了一种简单的方法来读取和写入应用程序的配置数据,支持多种存储格式,包括 Windows 注册表、INI 文件和 XML 文件等。
主要功能
1. 存储设置:可以存储各种类型的数据,包括字符串、整数、布尔值等。
2. 读取设置:可以方便地读取之前存储的设置。
3. 跨平台:在不同操作系统上,QSettings 会自动选择合适的存储方式。
4. 分组管理:可以使用组(group)来组织设置,便于管理。
转载请附上文章出处与本文链接。
QSettings 使用详解目录
1 读写ini文件
1.1 读取ini文件
1.2 写ini文件
1.3 .h代码
1.4 .cpp代码
1.5 ini文件
2 读写注册表
2.1 说明
2.2 创建QSttings
2.3 读取注册表
2.4 写注册表
2.5 .h
2.6 .cpp
3 完整内容
3 .h
3.2 .cpp
4 其它文章
1 读写ini文件
1.1 读取ini文件
void QSettingsTest::readSettings(SettingInfo &Info)
{
Info.miIndex = mSettings->value("Setting/add1", 0).toInt();
Info.msUserName = mSettings->value("UserInfo/name", 0).toString();
}
1.2 写ini文件
void QSettingsTest::writeSettings(SettingInfo &Info)
{
mSettings->setValue("Setting/add1", QString::number(Info.miIndex));
mSettings->setValue("UserInfo/name", Info.msUserName);
}
1.3 .h代码
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_QSettingsTest.h"
#include <QDebug>
#include <QSettings>
typedef struct SettingInfo
{
int miIndex = 0;
QString msUserName = "";
QString msText = "";
}SettingInfo;
class QSettingsTest : public QMainWindow
{
Q_OBJECT
public:
QSettingsTest(QWidget *parent = nullptr);
~QSettingsTest();
private:
void writeSettings(SettingInfo &Info);
void readSettings(SettingInfo &Info);
private:
Ui::QSettingsTestClass ui;
QSettings *mSettings;
QString msFilePath = "";
SettingInfo mSettingInfo;
};
1.4 .cpp代码
#include "QSettingsTest.h"
QSettingsTest::QSettingsTest(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
msFilePath = QCoreApplication::applicationDirPath() + "/config.ini";
mSettings = new QSettings(msFilePath, QSettings::IniFormat);
writeSettings(mSettingInfo);
}
QSettingsTest::~QSettingsTest()
{}
void QSettingsTest::writeSettings(SettingInfo &Info)
{
mSettings->setValue("Setting/add1", QString::number(Info.miIndex));
mSettings->setValue("UserInfo/name", Info.msUserName);
}
void QSettingsTest::readSettings(SettingInfo &Info)
{
Info.miIndex = mSettings->value("Setting/add1", 0).toInt();
Info.msUserName = mSettings->value("UserInfo/name", 0).toString();
}
1.5 ini文件
config.ini
[Setting]
add1=1
[UserInfo]
name=user
2 读写注册表
2.1 说明
windows + R 打开运行
输入 regedit 打开注册表编辑
选择新建一个测试值
操作注册表注意需要管理员权限
2.2 创建QSttings
msFilePath = QCoreApplication::applicationDirPath() + "/config.ini";
mSettings = new QSettings("HKEY_USERS\\QMConfig", QSettings::NativeFormat);
QSettings::NativeFormat:表示使用本地格式(注册表)。
QSettings::UserScope:表示设置是针对当前用户的。
2.3 读取注册表
void QSettingsTest::readSettings(SettingInfo &Info)
{
//Info.miIndex = mSettings->value("Setting/add1", 0).toInt();
//Info.msUserName = mSettings->value("UserInfo/name", 0).toString();
Info.miIndex = mSettings->value("Test", -1).toInt();
}
2.4 写注册表
void QSettingsTest::writeSettings(SettingInfo &Info)
{
//mSettings->setValue("Setting/add1", QString::number(Info.miIndex));
//mSettings->setValue("UserInfo/name", Info.msUserName);
mSettings->setValue("Test", QString::number(Info.miIndex));
}
2.5 .h
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_QSettingsTest.h"
#include <QDebug>
#include <QSettings>
typedef struct SettingInfo
{
int miIndex = 0;
QString msUserName = "";
QString msText = "";
}SettingInfo;
class QSettingsTest : public QMainWindow
{
Q_OBJECT
public:
QSettingsTest(QWidget *parent = nullptr);
~QSettingsTest();
private:
void writeSettings(SettingInfo &Info);
void readSettings(SettingInfo &Info);
private:
Ui::QSettingsTestClass ui;
QSettings *mSettings;
QString msFilePath = "";
SettingInfo mSettingInfo;
};
2.6 .cpp
#include "QSettingsTest.h"
QSettingsTest::QSettingsTest(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
msFilePath = QCoreApplication::applicationDirPath() + "/config.ini";
mSettings = new QSettings("HKEY_USERS\\QMConfig", QSettings::NativeFormat);
readSettings(mSettingInfo);
mSettingInfo.miIndex = 100;
writeSettings(mSettingInfo);
}
QSettingsTest::~QSettingsTest()
{}
void QSettingsTest::writeSettings(SettingInfo &Info)
{
//mSettings->setValue("Setting/add1", QString::number(Info.miIndex));
//mSettings->setValue("UserInfo/name", Info.msUserName);
mSettings->setValue("Test", QString::number(Info.miIndex));
}
void QSettingsTest::readSettings(SettingInfo &Info)
{
//Info.miIndex = mSettings->value("Setting/add1", 0).toInt();
//Info.msUserName = mSettings->value("UserInfo/name", 0).toString();
Info.miIndex = mSettings->value("Test", -1).toInt();
}
3 完整内容
3 .h
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_QSettingsTest.h"
#include <QDebug>
#include <QSettings>
typedef struct SettingInfo
{
int miIndex = 0;
QString msUserName = "";
QString msText = "";
}SettingInfo;
class QSettingsTest : public QMainWindow
{
Q_OBJECT
public:
QSettingsTest(QWidget *parent = nullptr);
~QSettingsTest();
private:
void writeSettings(SettingInfo &Info);
void readSettings(SettingInfo &Info);
private:
Ui::QSettingsTestClass ui;
QSettings *mSettings;
QString msFilePath = "";
SettingInfo mSettingInfo;
};
3.2 .cpp
#include "QSettingsTest.h"
QSettingsTest::QSettingsTest(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
msFilePath = QCoreApplication::applicationDirPath() + "/config.ini";
mSettings = new QSettings(msFilePath, QSettings::NativeFormat);
qDebug() << "Config file path:" << msFilePath;
}
QSettingsTest::~QSettingsTest()
{}
void QSettingsTest::writeSettings(SettingInfo &Info)
{
//mSettings->setValue("Setting/add1", QString::number(Info.miIndex));
//mSettings->setValue("UserInfo/name", Info.msUserName);
//mSettings->setValue("Test", QString::number(Info.miIndex));
if (!mSettings) {
qDebug() << "Failed to create QSettings object.";
}
}
void QSettingsTest::readSettings(SettingInfo &Info)
{
//Info.miIndex = mSettings->value("Setting/add1", 0).toInt();
//Info.msUserName = mSettings->value("UserInfo/name", 0).toString();
//Info.miIndex = mSettings->value("Test", -1).toInt();
if (!mSettings) {
qDebug() << "Failed to create QSettings object.";
}
}
4 其它文章
QT基本类使用详解系列文章包括以下内容:
QUUID 使用详解-CSDN博客
QDateTime 使用详解-CSDN博客