QSetting读写配置文件的基本使用

2023-11-06 20:30

本文主要是介绍QSetting读写配置文件的基本使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在Qt开发过程中,有时候需要我们保存一些软件的信息,比如:资源版本号,固件版本,注册码等信息,一般我们可能写到配置文件xml或json文件中,其实Qt有自带的QSetting类可以写到ini文件中,QSetting功能还是比较全面的,既可以写到本地文件中,也可写注册表,本文介绍它的基本用法。

1.写到本地文件中。
QSettings settings("aSetting.ini", QSettings::IniFormat, this);
aSetting.ini是文件名,QSettings::IniFormat表明是文件格式以后缀ini结尾。
代码示例如下:
写ini文件:

QSettings settings("aSetting.ini", QSettings::IniFormat, this);
settings.setValue("name", "Scott");
settings.setValue("height", 174.6);
settings.setValue("weight", 58.6);

这里没有分组,默认为[General]组。 

读ini文件:

QSettings settings("aSetting.ini", QSettings::IniFormat, this);
QString name = settings.value("name", "").toString();           //默认值为空
float height = settings.value("height", 1.6f).toFloat();        //默认值为1.6
float weight = settings.value("weight", 18.6f).toFloat();       //默认值为18.6
qDebug() << "slotReadSetting======name====" << name << " height=" << height << " weight=" << weight;

2.以beginGroup,endGroup分组的方式读写ini文件
写ini文件:

    QSettings settings("aSetting2.ini", QSettings::IniFormat, this);settings.setIniCodec(QTextCodec::codecForName("GB2312"));settings.beginGroup("string");settings.setValue("name", QStringLiteral("何静"));settings.setValue("sex", QStringLiteral("男"));settings.setValue("nation", QStringLiteral("汉族"));settings.endGroup();settings.beginGroup("data");settings.setValue("age", 30);settings.setValue("height", 173.1);settings.setValue("weight", 62.2);settings.endGroup();

这里有两个分组,string组和data组。 


读ini文件:

    QSettings settings("aSetting2.ini", QSettings::IniFormat, this);settings.beginGroup("string");QString name = settings.value("name", "").toString();//默认值为空QString sex = settings.value("sex", "").toString();//默认值为空QString nation = settings.value("nation", "汉族").toString();//默认值为"汉族"settings.endGroup();settings.beginGroup("data");int age = settings.value("age", 0).toInt();float height = settings.value("height", 0.0f).toFloat();float weight = settings.value("weight", 0.0f).toFloat();settings.endGroup();qDebug() << "slotTitleReadSetting======name====" << name << " sex=" << sex << " nation=" << nation;qDebug() << "slotTitleReadSetting======age=====" << age << " height=" << height << " weight=" << weight;

3.也可以不用beginGroup,endGroup分组,在读写ini文件的时候把组名加到key的前面。
写ini文件:

    QSettings settings("aSetting3.ini", QSettings::IniFormat, this);settings.setIniCodec(QTextCodec::codecForName("GB2312"));settings.setValue("string/name", QStringLiteral("金玉清"));settings.setValue("string/sex", QStringLiteral("女"));settings.setValue("string/nation", QStringLiteral("朝鲜族"));settings.setValue("data/age", 33);settings.setValue("data/height", 163.1);settings.setValue("data/weight", 52.2);


读ini文件:

    QSettings settings("aSetting3.ini", QSettings::IniFormat, this);QString name = settings.value("string/name", "").toString();QString sex = settings.value("string/sex", "").toString();QString nation = settings.value("string/nation", "汉族").toString();int age = settings.value("data/age", 0).toInt();float height = settings.value("data/height", 0.0f).toFloat();float weight = settings.value("data/weight", 0.0f).toFloat();qDebug() << "slotLayerReadSetting======name====" << name << " sex=" << sex << " nation=" << nation;qDebug() << "slotLayerReadSetting======age=====" << age << " height=" << height << " weight=" << weight;

4.QSetting读写注册表,根据构造函数,前一个参数是组织名,后一个参数是应用名:
QSettings(const QString &organization, const QString &application = QString(), QObject *parent = nullptr);
下面看代码示例:
写注册表:

    QSettings settings("Wumu Soft", "Clipper");settings.setValue("version", "2.6.5");settings.setValue("lastTime", 1697190536);

 

读注册表:

    QSettings settings("Wumu Soft", "Clipper");QString version = settings.value("version").toString();qint64 lastTime = settings.value("lastTime").toLongLong();qDebug() << "version==============" << version << " lastTime=====" << lastTime;


完整代码如下:

#ifndef QSETTINGDEMO_H
#define QSETTINGDEMO_H#include <QMainWindow>QT_BEGIN_NAMESPACE
namespace Ui { class QSettingDemo; }
QT_END_NAMESPACEclass QSettingDemo : public QMainWindow
{Q_OBJECTpublic:QSettingDemo(QWidget *parent = nullptr);~QSettingDemo();void initData();public slots:void slotReadSetting();void slotWriteSetting();void slotTitleReadSetting();void slotTitleWriteSetting();void slotLayerReadSetting();void slotLayerWriteSetting();void slotReadRegedit();void slotWriteRegedit();private:Ui::QSettingDemo *ui;
};
#endif // QSETTINGDEMO_H
#include "qsettingdemo.h"
#include "ui_qsettingdemo.h"
#include <QSettings>
#include <QDebug>
#include <QTextCodec>
#include <QIntValidator>QSettingDemo::QSettingDemo(QWidget *parent): QMainWindow(parent), ui(new Ui::QSettingDemo)
{ui->setupUi(this);initData();
}QSettingDemo::~QSettingDemo()
{delete ui;
}void QSettingDemo::initData()
{QValidator *validator = new QIntValidator(0, 9, ui->lineEdit);// the edit lineedit will only accept integers between 100 and 999ui->lineEdit->setValidator(validator);connect(ui->btnRead, SIGNAL(clicked()), this, SLOT(slotReadSetting()));connect(ui->btnWrite, SIGNAL(clicked()), this, SLOT(slotWriteSetting()));connect(ui->btnReadTitle, SIGNAL(clicked()), this, SLOT(slotTitleReadSetting()));connect(ui->btnWriteTitle, SIGNAL(clicked()), this, SLOT(slotTitleWriteSetting()));connect(ui->btnReadLayer, SIGNAL(clicked()), this, SLOT(slotLayerReadSetting()));connect(ui->btnWriteLayer, SIGNAL(clicked()), this, SLOT(slotLayerWriteSetting()));connect(ui->btnReadRegedit, SIGNAL(clicked()), this, SLOT(slotReadRegedit()));connect(ui->btnWriteRegedit, SIGNAL(clicked()), this, SLOT(slotWriteRegedit()));
}void QSettingDemo::slotReadSetting()
{QSettings settings("aSetting.ini", QSettings::IniFormat, this);QString name = settings.value("name", "").toString();           //默认值为空float height = settings.value("height", 1.6f).toFloat();        //默认值为1.6float weight = settings.value("weight", 18.6f).toFloat();       //默认值为18.6qDebug() << "slotReadSetting======name====" << name << " height=" << height << " weight=" << weight;
}void QSettingDemo::slotWriteSetting()
{QSettings settings("aSetting.ini", QSettings::IniFormat, this);settings.setValue("name", "Scott");settings.setValue("height", 174.6);settings.setValue("weight", 58.6);}void QSettingDemo::slotTitleReadSetting()
{QSettings settings("aSetting2.ini", QSettings::IniFormat, this);//settings.setIniCodec(QTextCodec::codecForName("UTF-8"));settings.beginGroup("string");QString name = settings.value("name", "").toString();//默认值为空QString sex = settings.value("sex", "").toString();//默认值为空QString nation = settings.value("nation", "汉族").toString();//默认值为"汉族"settings.endGroup();settings.beginGroup("data");int age = settings.value("age", 0).toInt();float height = settings.value("height", 0.0f).toFloat();float weight = settings.value("weight", 0.0f).toFloat();settings.endGroup();qDebug() << "slotTitleReadSetting======name====" << name << " sex=" << sex << " nation=" << nation;qDebug() << "slotTitleReadSetting======age=====" << age << " height=" << height << " weight=" << weight;
}void QSettingDemo::slotTitleWriteSetting()
{QSettings settings("aSetting2.ini", QSettings::IniFormat, this);settings.setIniCodec(QTextCodec::codecForName("GB2312"));settings.beginGroup("string");settings.setValue("name", QStringLiteral("何静"));settings.setValue("sex", QStringLiteral("男"));settings.setValue("nation", QStringLiteral("汉族"));settings.endGroup();settings.beginGroup("data");settings.setValue("age", 30);settings.setValue("height", 173.1);settings.setValue("weight", 62.2);settings.endGroup();
}void QSettingDemo::slotLayerReadSetting()
{QSettings settings("aSetting3.ini", QSettings::IniFormat, this);//settings.setIniCodec(QTextCodec::codecForName("UTF-8"));QString name = settings.value("string/name", "").toString();QString sex = settings.value("string/sex", "").toString();QString nation = settings.value("string/nation", "汉族").toString();int age = settings.value("data/age", 0).toInt();float height = settings.value("data/height", 0.0f).toFloat();float weight = settings.value("data/weight", 0.0f).toFloat();qDebug() << "slotLayerReadSetting======name====" << name << " sex=" << sex << " nation=" << nation;qDebug() << "slotLayerReadSetting======age=====" << age << " height=" << height << " weight=" << weight;
}void QSettingDemo::slotLayerWriteSetting()
{QSettings settings("aSetting3.ini", QSettings::IniFormat, this);settings.setIniCodec(QTextCodec::codecForName("GB2312"));settings.setValue("string/name", QStringLiteral("金玉清"));settings.setValue("string/sex", QStringLiteral("女"));settings.setValue("string/nation", QStringLiteral("朝鲜族"));settings.setValue("data/age", 33);settings.setValue("data/height", 163.1);settings.setValue("data/weight", 52.2);
}void QSettingDemo::slotReadRegedit()
{QSettings settings("Wumu Soft", "Clipper");QString version = settings.value("version").toString();qint64 lastTime = settings.value("lastTime").toLongLong();qDebug() << "version==============" << version << " lastTime=====" << lastTime;
}void QSettingDemo::slotWriteRegedit()
{QSettings settings("Wumu Soft", "Clipper");settings.setValue("version", "2.6.5");settings.setValue("lastTime", 1697190536);
}

这篇关于QSetting读写配置文件的基本使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/358878

相关文章

使用Java解析JSON数据并提取特定字段的实现步骤(以提取mailNo为例)

《使用Java解析JSON数据并提取特定字段的实现步骤(以提取mailNo为例)》在现代软件开发中,处理JSON数据是一项非常常见的任务,无论是从API接口获取数据,还是将数据存储为JSON格式,解析... 目录1. 背景介绍1.1 jsON简介1.2 实际案例2. 准备工作2.1 环境搭建2.1.1 添加

如何使用celery进行异步处理和定时任务(django)

《如何使用celery进行异步处理和定时任务(django)》文章介绍了Celery的基本概念、安装方法、如何使用Celery进行异步任务处理以及如何设置定时任务,通过Celery,可以在Web应用中... 目录一、celery的作用二、安装celery三、使用celery 异步执行任务四、使用celery

使用Python绘制蛇年春节祝福艺术图

《使用Python绘制蛇年春节祝福艺术图》:本文主要介绍如何使用Python的Matplotlib库绘制一幅富有创意的“蛇年有福”艺术图,这幅图结合了数字,蛇形,花朵等装饰,需要的可以参考下... 目录1. 绘图的基本概念2. 准备工作3. 实现代码解析3.1 设置绘图画布3.2 绘制数字“2025”3.3

Jsoncpp的安装与使用方式

《Jsoncpp的安装与使用方式》JsonCpp是一个用于解析和生成JSON数据的C++库,它支持解析JSON文件或字符串到C++对象,以及将C++对象序列化回JSON格式,安装JsonCpp可以通过... 目录安装jsoncppJsoncpp的使用Value类构造函数检测保存的数据类型提取数据对json数

python使用watchdog实现文件资源监控

《python使用watchdog实现文件资源监控》watchdog支持跨平台文件资源监控,可以检测指定文件夹下文件及文件夹变动,下面我们来看看Python如何使用watchdog实现文件资源监控吧... python文件监控库watchdogs简介随着Python在各种应用领域中的广泛使用,其生态环境也

Python中构建终端应用界面利器Blessed模块的使用

《Python中构建终端应用界面利器Blessed模块的使用》Blessed库作为一个轻量级且功能强大的解决方案,开始在开发者中赢得口碑,今天,我们就一起来探索一下它是如何让终端UI开发变得轻松而高... 目录一、安装与配置:简单、快速、无障碍二、基本功能:从彩色文本到动态交互1. 显示基本内容2. 创建链

springboot整合 xxl-job及使用步骤

《springboot整合xxl-job及使用步骤》XXL-JOB是一个分布式任务调度平台,用于解决分布式系统中的任务调度和管理问题,文章详细介绍了XXL-JOB的架构,包括调度中心、执行器和Web... 目录一、xxl-job是什么二、使用步骤1. 下载并运行管理端代码2. 访问管理页面,确认是否启动成功

使用Nginx来共享文件的详细教程

《使用Nginx来共享文件的详细教程》有时我们想共享电脑上的某些文件,一个比较方便的做法是,开一个HTTP服务,指向文件所在的目录,这次我们用nginx来实现这个需求,本文将通过代码示例一步步教你使用... 在本教程中,我们将向您展示如何使用开源 Web 服务器 Nginx 设置文件共享服务器步骤 0 —

Java中switch-case结构的使用方法举例详解

《Java中switch-case结构的使用方法举例详解》:本文主要介绍Java中switch-case结构使用的相关资料,switch-case结构是Java中处理多个分支条件的一种有效方式,它... 目录前言一、switch-case结构的基本语法二、使用示例三、注意事项四、总结前言对于Java初学者

Golang使用minio替代文件系统的实战教程

《Golang使用minio替代文件系统的实战教程》本文讨论项目开发中直接文件系统的限制或不足,接着介绍Minio对象存储的优势,同时给出Golang的实际示例代码,包括初始化客户端、读取minio对... 目录文件系统 vs Minio文件系统不足:对象存储:miniogolang连接Minio配置Min