QSettings类对配置文件进行读写操作

用户对应用程序经常有这样的要求:要求它能记住它的settings,比如窗口大小,位置,一些别的设置,还有一个经常用的,就是recent files,等等这些都可以通过Qsettings来实现。

我们知道,这些settings一般都是存在系统里的,比如windows一般都写在系统注册表或者写INI文件,mac系统一般都在XML文件里,那么按照一般的标准来说,许多应用程序是用INI文件来实现的。而Qsettings就是提供了一种方便的方法来存储和恢复应用程序的settings。

#include "dialog.h"
#include "ui_dialog.h"
#include <QColor>
#include <QPainterPath>
#include <QPainter>
#include <QDebug>
#include <QFile>
#include <QSettings>


Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);

    // 获取配置文件的目录
    QString str = QApplication::applicationDirPath() + "/" + QString("tradeprintinfo.ini");
    QColor color(1,1,1);

	// QSettings::IniFormat:代表对配置文件进行读写操作
    QSettings settings(str, QSettings::IniFormat);
    // 往配置文件中写入rgb
    settings.setValue("DefaultSkin/red", color.red());
    settings.setValue("DefaultSkin/green", color.green());
    settings.setValue("DefaultSkin/blue", color.blue());

    
    QColor color1;
    // 从配置文件中拿出rgb
    color1.setRed(settings.value("DefaultSkin/red").toInt());
    color1.setGreen(settings.value("DefaultSkin/green").toInt());
    color1.setBlue(settings.value("DefaultSkin/blue").toInt());

    qDebug() << color1.red() << "  " << color1.green() << "  " << color1.blue(); // 1 1 1

}
Dialog::~Dialog()
{
    delete ui;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值