头文件Config.h

#pragma once

#include <QVariantMap>

class Config
{
public:
Config(const QString &fileName);
~Config(); bool open(const QString &fileName);
void sync(); void write(const QString &key, const QVariant& value); QString readString(const QString &key, const QString &default = "");
bool readBool(const QString &key, bool default = false);
int readInt(const QString &key, int default = ); private:
QString m_fileName; QVariantMap m_cache;
};

源文件Config.cpp

#include "Config.h"

#include <QFile>
#include <QJsonDocument>
#include <QJsonObject> Config::Config(const QString &fileName)
: m_fileName(fileName)
{
open(fileName);
} Config::~Config()
{
sync();
} bool Config::open(const QString &fileName)
{
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly))
{
return false;
}
QByteArray allData = file.readAll();
file.close(); QJsonParseError jsonError;
QJsonDocument jsonDoc = QJsonDocument::fromJson(allData, &jsonError);
if (jsonError.error != QJsonParseError::NoError)
{
return false;
} QJsonObject root = jsonDoc.object();
m_cache = root.toVariantMap(); return true;
} void Config::sync()
{
QJsonObject root = QJsonObject::fromVariantMap(m_cache);
QJsonDocument jsonDoc(root);
QByteArray data = jsonDoc.toJson(QJsonDocument::Compact);
QFile file(m_fileName);
if (file.open(QIODevice::WriteOnly))
{
file.write(data);
file.close();
}
} void Config::write(const QString &key, const QVariant &value)
{
m_cache.insert(key, value);
} QString Config::readString(const QString &key, const QString &default)
{
if (m_cache.contains(key))
{
return m_cache.value(key).toString();
} return default;
} bool Config::readBool(const QString &key, bool default)
{
if (m_cache.contains(key))
{
return m_cache.value(key).toBool();
} return default;
} int Config::readInt(const QString &key, int default)
{
if (m_cache.contains(key))
{
return m_cache.value(key).toInt();
} return default;
}

Qt读写Json格式配置文件的更多相关文章

  1. Android读写JSON格式的数据之JsonWriter和JsonReader

    近期的好几个月都没有搞Android编程了,逐渐的都忘却了一些东西.近期打算找一份Android的工作,要继续拾起曾经的东西.公司月初搬家之后就一直没有网络,直到今日公司才有网络接入,各部门才開始办公 ...

  2. 读写JSON作配置文件

    个人不太喜欢XML,于是找了JSON来做配置,JSON虽然有很多引号,但这种key-value的形式,非常符合我的思维,就像是一个萝卜一个坑.最近在读写JSON文件,需要注意两个问题. 中文乱码: 直 ...

  3. Qt读写Json

    Qt操作Json 1.QJsonDocument 1.详细说明 QJsonDocument类提供了读写JSON文档的方法. QJsonDocument是一个封装了完整JSON文档的类,可以从基于UTF ...

  4. QJsonDocument实现Qt下JSON文档读写

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QJsonDocument实现Qt下JSON文档读写     本文地址:http://tech ...

  5. 纯C#的ini格式配置文件读写

    虽然C#里都是添加app.config 并且访问也很方便 ,有时候还是不习惯用他.那么我们来做个仿C++下的那种ini配置文件读写吧,其他人写的都是调用非托管kernel32.dll.我也用过 但是感 ...

  6. .net core读取json格式的配置文件

    在.Net Framework中,配置文件一般采用的是XML格式的,.NET Framework提供了专门的ConfigurationManager来读取配置文件的内容,.net core中推荐使用j ...

  7. C语言ini格式配置文件的读写

    依赖的类 /*1 utils.h *# A variety of utility functions. *# *# Some of the functions are duplicates of we ...

  8. python 读写json文件(dump, load),以及对json格式的数据处理(dumps, loads)

    JSON (JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. 1.json.dumps()和json.loads()是json ...

  9. Delphi中Json格式读写

    Json是一种轻量级传输数据格式,广泛应用互联网和各应用中.json主要採用键值对来表示数据项.多个数据项之间用逗号分隔,也能够用于数组.以下注重介绍一下在delphi中使用json,在delphi中 ...

随机推荐

  1. webapi IHttpActionResult无引用和config.MapHttpAttributeRoutes()无引用解决办法

    1. 打开NuGet,打开方法 工具->库程序包管理器->程序包管理器控制台,如下图所示: 2. 输入如下命令Install-Package Microsoft.AspNet.WebApi ...

  2. Keras 笔记

    1. 从 meta 模型恢复graph,   修改node  并保存 from __future__ import absolute_import from __future__ import div ...

  3. Android ANR总结

    1.ANR定义 ANR的全称是application not responding,是指应用程序未响应,Android系统对于一些事件需要在一定的时间范围内完成,如果超过预定时间未能得到有效响应或者响 ...

  4. springboot 部署到tomcat,获取根路径问题。空格变为%20

    String path = ResourceUtils.getURL("classpath:").getPath()+"static/upload"; Syst ...

  5. c#通用语言运行时CLR

  6. 《python解释器源码剖析》第12章--python虚拟机中的函数机制

    12.0 序 函数是任何一门编程语言都具备的基本元素,它可以将多个动作组合起来,一个函数代表了一系列的动作.当然在调用函数时,会干什么来着.对,要在运行时栈中创建栈帧,用于函数的执行. 在python ...

  7. 什么是领域模型(domain model)?贫血模型(anaemic domain model)和充血模型(rich domain model)有什么区别

    领域模型是领域内的概念类或现实世界中对象的可视化表示,又称为概念模型或分析对象模型,它专注于分析问题领域本身,发掘重要的业务领域概念,并建立业务领域概念之间的关系. 贫血模型是指使用的领域对象中只有s ...

  8. python zipfile使用

    the Python challenge中第6关使用到zipfile模块,于是记录下zipfile的使用 zip日常使用只要是压缩跟解压操作,于是从这里入手 1.压缩 f=zipfile.ZipFil ...

  9. python3 matplotlib

    一.matplotlib数据可视化 1.https://matplotlib.org/ 2.figure图形窗口:figsize窗口大小,label轴标签:title标题:lim限制:plot绘图:s ...

  10. linux实操_shell流程控制

    if判断: 基本语法: if [ 条件判断式 ] then 程序 elif [ 条件判断式 ] then 程序 fi 实例:请编写一个shell程序,如果输入的参数,大于60,则输出“及格了”,如果小 ...