Qt读写Json格式配置文件
头文件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格式配置文件的更多相关文章
- Android读写JSON格式的数据之JsonWriter和JsonReader
近期的好几个月都没有搞Android编程了,逐渐的都忘却了一些东西.近期打算找一份Android的工作,要继续拾起曾经的东西.公司月初搬家之后就一直没有网络,直到今日公司才有网络接入,各部门才開始办公 ...
- 读写JSON作配置文件
个人不太喜欢XML,于是找了JSON来做配置,JSON虽然有很多引号,但这种key-value的形式,非常符合我的思维,就像是一个萝卜一个坑.最近在读写JSON文件,需要注意两个问题. 中文乱码: 直 ...
- Qt读写Json
Qt操作Json 1.QJsonDocument 1.详细说明 QJsonDocument类提供了读写JSON文档的方法. QJsonDocument是一个封装了完整JSON文档的类,可以从基于UTF ...
- QJsonDocument实现Qt下JSON文档读写
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QJsonDocument实现Qt下JSON文档读写 本文地址:http://tech ...
- 纯C#的ini格式配置文件读写
虽然C#里都是添加app.config 并且访问也很方便 ,有时候还是不习惯用他.那么我们来做个仿C++下的那种ini配置文件读写吧,其他人写的都是调用非托管kernel32.dll.我也用过 但是感 ...
- .net core读取json格式的配置文件
在.Net Framework中,配置文件一般采用的是XML格式的,.NET Framework提供了专门的ConfigurationManager来读取配置文件的内容,.net core中推荐使用j ...
- C语言ini格式配置文件的读写
依赖的类 /*1 utils.h *# A variety of utility functions. *# *# Some of the functions are duplicates of we ...
- python 读写json文件(dump, load),以及对json格式的数据处理(dumps, loads)
JSON (JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. 1.json.dumps()和json.loads()是json ...
- Delphi中Json格式读写
Json是一种轻量级传输数据格式,广泛应用互联网和各应用中.json主要採用键值对来表示数据项.多个数据项之间用逗号分隔,也能够用于数组.以下注重介绍一下在delphi中使用json,在delphi中 ...
随机推荐
- 【es6】将2个数组合并为一个数组
//第一种 一个数组中的值为key 一个数组中的值为value let arr1 = ['内存','颜色','尺寸']; let arr2 = [1,2,3]; let temp = arr1.map ...
- pytorch报错:AttributeError: 'module' object has no attribute '_rebuild_tensor_v2'
转载自: https://blog.csdn.net/qq_24305433/article/details/80844548 由于训练模型时使用的是新版本的pytorch,而加载时使用的是旧版本的p ...
- Vue中遍历数组的新方法
1.foreach foreach循环对不能使用return来停止循环 search(keyword){ var newList = [] this.urls.forEach(item =>{ ...
- 【vue开发】vue插件的install方法
MyPlugin.install = function (Vue, options) { // 1. 添加全局方法或属性 Vue.myGlobalMethod = function () { // 逻 ...
- Linux报错排解
1.Loaded plugins: product-id, search-disabled-repos, subscription-manager This system is not registe ...
- 【Redis】分布式Session
一.问题引出 1.1 Session的原理 1.2 问题概述 二.解决方案 三.代码实现-使用Token代替Session 3.1 Service 3.2 TokenController 一.问题引出 ...
- 后台向前台响应的json数据格式的一些问题
最近在写后台向前台easyUI页面发送数据时遇到的一些报错. 首先easyUI内部封装了许多的方法和对象,以至于很多参数都不清楚,需要查询,其次easyUI也是有内置ajax所以从后台响应回来的数据一 ...
- 【OGG 故障处理】OGG-01028
通过ATSCN 的方式启动REPLICAT 进程的时候报错 GGSCI> START REPLICAT RP_XXXX1, ATCSN 15572172378 GGSCI> VIEW RE ...
- flash多进程写操作
1 应用场景介绍 硬件条件:使用stm32 MCU 软件条件:协议栈应用 协议栈简单介绍如下: 类似于OSI七层模型,所涉及的协议栈包括应用层,网络层,链路层,物理层,如下图: 在 ...
- tcpdump和windump
Tcpdump简介 tcpdump命令是一款sniffer工具,它可以打印所有经过网络接口的数据包的头信息, tcpdump,就是:dump the traffic on a network,根据使用 ...