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中 ...
随机推荐
- (二)Spring框架之JDBC的基本使用(p6spy插件的使用)
案例一: 用Spring IOC方式使用JDBC Test_2.java package jdbc; import java.lang.Thread.State; import java.sql.Co ...
- vue引入警告:There are multiple modules with names that only differ in casing. This can lead to unexpected behavior when compiling on a filesystem with other case-semantic. Use equal casing. Compare these
在写vue项目的时候 当我使用 : import dataSource from '../overseaProduct/house/dataSource'; 引入dataSource文件的时候:控制台 ...
- 配置Hadoop,hive,spark,hbase ————待整理
五一一天在家搭建好了集群,要上班了来不及整理,待下周周末有时间好好整理整理一个完整的搭建hadoop生态圈的集群的系列 若出现license information(license not accep ...
- webbrowser 屏蔽脚本错误
webBrowser1.ScriptErrorsSuppressed = true
- mysql5.6快速安装及参数详解
一.所需软件 软件名称 版本 下载地址 当前环境 管理员账号/密码 mysql 5.6 yum安装 centOS6.7系统 zxfly/zxfly 二.安装说明 数据库所在目录 /database/m ...
- JAVA Calendar类获取上个月的第一天和最后一天
原文:https://www.cnblogs.com/QQParadise/articles/4936313.html 获取上个月第一天的方法: Calendar calendar = Calenda ...
- synchronized 和 ReentrantLock 区别是什么?(未完成)
synchronized 和 ReentrantLock 区别是什么?(未完成)
- tsp问题-遍历算法/随机算法
旅行商问题,即TSP问题(Traveling Salesman Problem)又译为旅行推销员问题.货郎担问题,是数学领域中著名问题之一.假设有一个旅行商人要拜访n个城市,他必须选择所要走的路径,路 ...
- python中使用uwsgi启动wsgi应用
uwsgi --http :8000 --wsgi-file wxhttpapi2.py --callable application --processes 4 --threads 2
- python+mysql:实现一千万条数据插入数据库
作业要求 构建一个关系模式和课本中的关系movies(title,year,length,movietype,studioname,producerC)一样的关系,名称自定,在这个关系中插入1000万 ...