QT 使用QSettings读写ini配置文件
利用Qsettings包一个类 RWIniFile, writeIni方法写文件, readIni方法读文件
rwinifile.h
#ifndef RWINIFILE_H
#define RWINIFILE_H #include <QtGui>
class RWIniFile
{
public:
RWIniFile();
bool static writeIni(QString path, QString section,QString keyword, QString keyvalue,bool clear_flag);
bool static writeIni(QString path, QString section,QString keyword, QString keyvalue);
QString static readIni(QString path, QString section,QString keyword, QString &keyvalue);
};
#endif // RWINIFILE_H
rwinifile.cpp
#include "rwinifile.h" RWIniFile::RWIniFile()
{
} bool RWIniFile::writeIni(QString path, QString section,QString keyword, QString keyvalue)
{
if(path == QString(""))
{
return false;
}
else
{
//创建配置文件操作对象
QSettings *config = new QSettings(path, QSettings::IniFormat); //将信息写入配置文件
config->beginGroup(section);
config->setValue(keyword, keyvalue);
config->endGroup(); if(config)
{
delete config;
} return true;
}
} bool RWIniFile::writeIni(QString path, QString section,QString keyword, QString keyvalue, bool clear_flag)
{
if(path == QString(""))
{
return false;
}
else
{
//创建配置文件操作对象
QSettings *config = new QSettings(path, QSettings::IniFormat);
if( clear_flag == true )
config->clear(); //将信息写入配置文件
config->beginGroup(section);
config->setValue(keyword, keyvalue);
config->endGroup(); if(config)
{
delete config;
} return true;
}
} QString RWIniFile::readIni(QString path, QString section,QString keyword, QString &keyvalue)
{
if(path == QString(""))
{
return false;
}
else
{
//创建配置文件操作对象
QSettings *config = new QSettings(path, QSettings::IniFormat); keyvalue = config->value(section + "/" + keyword).toString(); if(config)
{
delete config;
} return keyvalue;
}
}
main函数里的使用方法
RWIniFile::writeIni("log.txt", "config1", "key1", "test");
RWIniFile::writeIni("log.txt", "config1", "key2", "test2");
RWIniFile::writeIni("log.txt", "config2", "key1", "test");
RWIniFile::writeIni("log.txt", "config2", "key2", "test2");
RWIniFile::writeIni("log.txt", "config2", "key2", "test2"); //写入不清除原配置
RWIniFile::writeIni("log.txt", "config3", "key", "hello world", false); //如果clear_flag=true,清除原配置
QString keyword;
keyword = RWIniFile::readIni(QString("log.txt"), QString("config3"), QString("key"), keyword); //读配置文档
qDebug() << keyword ;
QT 使用QSettings读写ini配置文件的更多相关文章
- 【Qt 】QSettings写ini配置文件
QSettings写ini配置文件(Qt版本5.2): #include "inidemo.h" #include <QSettings> #include <Q ...
- C# 读写 ini 配置文件
虽说 XML 文件越发流行,但精简的 ini 配置文件还是经常会用到,在此留个脚印. 当然,文中只是调用系统API,不会报错,如有必要,也可以直接以流形式读取 ini文件并解析. /// <su ...
- [转]VB 读写ini 配置文件
转自 百度知道 C# 读写 ini配置文件 点此链接 'API 声明Public Declare Function GetPrivateProfileString Lib "kernel32 ...
- 自己写的 读写 ini 配置文件类
/// <summary> /// 不调用系统API 读写 ini 配置文件 /// </summary> public class RW_ini { #region ==== ...
- 引用“kernel32”读写ini配置文件
引用"kernel32"读写ini配置文件 unity ini kernel32 配置文件 引用"kernel32"读写ini配置文件 OverView ke ...
- C# 文件的一些基本操作(转)//用C#读写ini配置文件
C# 文件的一些基本操作 2009-07-19 来自:博客园 字体大小:[大 中 小] 摘要:介绍C#对文件的一些基本操作,读写等. using System;using System.IO;us ...
- C#操作读写INI配置文件
一个完整的INI文件格式由节(section).键(key).值(value)组成.示例如:[section]key1=value1key2=value2; 备注:value的值不要太长,理论上最多不 ...
- Qt读写三种文件,QSettings读ini配置文件,QJsonDocument读JSON文件,QDomDocument读xml文件
第一种INI配置文件 .ini 文件是Initialization File的缩写,即初始化文件. 除了windows现在很多其他操作系统下面的应用软件也有.ini文件,用来配置应用软件以实现不同用户 ...
- QT读写ini配置文件
/********下面是写ini文件*************************/ //Qt中使用QSettings类读写ini文件 //QSettings构造函数的第一 ...
随机推荐
- 技术总结PHP+微信
ajax: //获取商品属性数据 function initGoodsAttr(){ $.ajax({ type: 'GET', url:"<?php ...
- java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\x87<b
实际开发中遇到的 情景: 解决方案: 复制的别人的博客,没测试, Incorrect string value: '\xF0\x9F...' for column 'XXX' at row 1 这个 ...
- 防sql注入之参数绑定 SQL Injection Attacks and Defense
http://php.net/manual/zh/pdo.prepared-statements.php 预处理语句与存储过程 很多更成熟的数据库都支持预处理语句的概念.什么是预处理语句?可以把它看作 ...
- Security Report: Stop using relative path to import CSS files
Detecting and exploiting path-relative stylesheet import (PRSSI) vulnerabilities Early last year G ...
- python面试题(七)
1 什么是局域网.广域网.城域网? ①局域网LAN(Local Area Network):一般指覆盖范围在10公里以内,一座楼房或一个单位内部的网络.由于传输距离直接影响传输速度,因此,局域网内的通 ...
- python多进程编程(一)
multiprocessing模块介绍 python中的多线程无法利用多核优势,如果想要充分地使用多核CPU的资源(os.cpu_count()查看),在python中大部分情况需要使用多进程.Pyt ...
- Tensorflow瞎搞
Tensorflow为张量从流图的一端流动到另一端的计算过程,Tensorflow也可以看成是将复杂的数据结构传输至人工智能神经网络中进行分析和处理的系统. 张量概念是矢量概念的推广,矢量是一阶张量. ...
- F-02 创建财务凭证BAPI
**.获取抬头参数, documentheader **.项目参数 accountgl = lt_acgl"G/L account item accountreceivable = lt_a ...
- corethink功能模块探索开发(五)开启这个模块的配置
上图: 主要就是两点. 1.在opencmf.php中填写好配置页面的按钮还是文本域 Equip/opencmf.php只需要注意主要的配置数组的内容 <?php // 模块信息配置 retur ...
- Python2 socket 多线程并发 TCPServer Demo
#coding=utf-8 import socket import threading,getopt,sys,string opts, args = getopt.getopt(sys.argv[1 ...