用户对应用程序经常有这样的要求:要求它能记住它的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;
}