一. ini文件是initialization file的缩写,即初始化文件,是widows系统配置文件所采用的存储格式。
配置文件tradeprintinfo.ini内容:
文件扩展名: .ini
ini配置文件的后缀名也不一定必须是.ini, 也可以是.cfg, .conf或者是.txt
ini文件格式
ini配置文件由参数, 节, 注解组成
[DefaultSkin];默认皮肤
red=218
green=67
blue=68
<1>参数(parameter)
以键值对的形式存在
red=218
green=67
blue=68
<2>节(section)
- 所有的键值对都是以节section为单位结合在一起的
- 所有的section名称都是独占一行,并且section名字都被方括号包围着[ ]
- 在section声明后的所有parameters都属于这个section
- 一个section没有明显的结束标识符,一个section的开始就是上一个section的结束
- section不能重复,数据通过section去查找,每个seletion下可以有多个key和value的键值对
[DefaultSkin]
<2>注解
使用分号表示, 在分号后面的文字,直到该行结尾全部为注解。
;默认皮肤
二. 项目应用
(1)读取配置文件信息
QColor CommonUtils::getDefaultSkinColor()
{
QColor color;
const QString && path = QApplication::applicationDirPath() + "/" + QString("tradeprintinfo.ini");
if (!QFile::exists(path))
setDefaultSkinColor(QColor(22, 154, 218));
QSettings settings(path, QSettings::IniFormat);
color.setRed(settings.value("DefaultSkin/red").toInt());
color.setGreen(settings.value("DefaultSkin/green").toInt());
color.setBlue(settings.value("DefaultSkin/blue").toInt());
return color;
}
(2)设置配置文件信息
void CommonUtils::setDefaultSkinColor(const QColor &color)
{
const QString && path = QApplication::applicationDirPath() + "/" + QString("tradeprintinfo.ini");
QSettings settings(path, QSettings::IniFormat);
settings.setValue("DefaultSkin/red", color.red());
settings.setValue("DefaultSkin/green", color.green());
settings.setValue("DefaultSkin/blue", color.blue());
}