本文主要是介绍QSetting读写配置文件的基本使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在Qt开发过程中,有时候需要我们保存一些软件的信息,比如:资源版本号,固件版本,注册码等信息,一般我们可能写到配置文件xml或json文件中,其实Qt有自带的QSetting类可以写到ini文件中,QSetting功能还是比较全面的,既可以写到本地文件中,也可写注册表,本文介绍它的基本用法。
1.写到本地文件中。
QSettings settings("aSetting.ini", QSettings::IniFormat, this);
aSetting.ini是文件名,QSettings::IniFormat表明是文件格式以后缀ini结尾。
代码示例如下:
写ini文件:
QSettings settings("aSetting.ini", QSettings::IniFormat, this);
settings.setValue("name", "Scott");
settings.setValue("height", 174.6);
settings.setValue("weight", 58.6);
这里没有分组,默认为[General]组。
读ini文件:
QSettings settings("aSetting.ini", QSettings::IniFormat, this);
QString name = settings.value("name", "").toString(); //默认值为空
float height = settings.value("height", 1.6f).toFloat(); //默认值为1.6
float weight = settings.value("weight", 18.6f).toFloat(); //默认值为18.6
qDebug() << "slotReadSetting======name====" << name << " height=" << height << " weight=" << weight;
2.以beginGroup,endGroup分组的方式读写ini文件
写ini文件:
QSettings settings("aSetting2.ini", QSettings::IniFormat, this);settings.setIniCodec(QTextCodec::codecForName("GB2312"));settings.beginGroup("string");settings.setValue("name", QStringLiteral("何静"));settings.setValue("sex", QStringLiteral("男"));settings.setValue("nation", QStringLiteral("汉族"));settings.endGroup();settings.beginGroup("data");settings.setValue("age", 30);settings.setValue("height", 173.1);settings.setValue("weight", 62.2);settings.endGroup();
这里有两个分组,string组和data组。
读ini文件:
QSettings settings("aSetting2.ini", QSettings::IniFormat, this);settings.beginGroup("string");QString name = settings.value("name", "").toString();//默认值为空QString sex = settings.value("sex", "").toString();//默认值为空QString nation = settings.value("nation", "汉族").toString();//默认值为"汉族"settings.endGroup();settings.beginGroup("data");int age = settings.value("age", 0).toInt();float height = settings.value("height", 0.0f).toFloat();float weight = settings.value("weight", 0.0f).toFloat();settings.endGroup();qDebug() << "slotTitleReadSetting======name====" << name << " sex=" << sex << " nation=" << nation;qDebug() << "slotTitleReadSetting======age=====" << age << " height=" << height << " weight=" << weight;
3.也可以不用beginGroup,endGroup分组,在读写ini文件的时候把组名加到key的前面。
写ini文件:
QSettings settings("aSetting3.ini", QSettings::IniFormat, this);settings.setIniCodec(QTextCodec::codecForName("GB2312"));settings.setValue("string/name", QStringLiteral("金玉清"));settings.setValue("string/sex", QStringLiteral("女"));settings.setValue("string/nation", QStringLiteral("朝鲜族"));settings.setValue("data/age", 33);settings.setValue("data/height", 163.1);settings.setValue("data/weight", 52.2);
读ini文件:
QSettings settings("aSetting3.ini", QSettings::IniFormat, this);QString name = settings.value("string/name", "").toString();QString sex = settings.value("string/sex", "").toString();QString nation = settings.value("string/nation", "汉族").toString();int age = settings.value("data/age", 0).toInt();float height = settings.value("data/height", 0.0f).toFloat();float weight = settings.value("data/weight", 0.0f).toFloat();qDebug() << "slotLayerReadSetting======name====" << name << " sex=" << sex << " nation=" << nation;qDebug() << "slotLayerReadSetting======age=====" << age << " height=" << height << " weight=" << weight;
4.QSetting读写注册表,根据构造函数,前一个参数是组织名,后一个参数是应用名:
QSettings(const QString &organization, const QString &application = QString(), QObject *parent = nullptr);
下面看代码示例:
写注册表:
QSettings settings("Wumu Soft", "Clipper");settings.setValue("version", "2.6.5");settings.setValue("lastTime", 1697190536);
读注册表:
QSettings settings("Wumu Soft", "Clipper");QString version = settings.value("version").toString();qint64 lastTime = settings.value("lastTime").toLongLong();qDebug() << "version==============" << version << " lastTime=====" << lastTime;
完整代码如下:
#ifndef QSETTINGDEMO_H
#define QSETTINGDEMO_H#include <QMainWindow>QT_BEGIN_NAMESPACE
namespace Ui { class QSettingDemo; }
QT_END_NAMESPACEclass QSettingDemo : public QMainWindow
{Q_OBJECTpublic:QSettingDemo(QWidget *parent = nullptr);~QSettingDemo();void initData();public slots:void slotReadSetting();void slotWriteSetting();void slotTitleReadSetting();void slotTitleWriteSetting();void slotLayerReadSetting();void slotLayerWriteSetting();void slotReadRegedit();void slotWriteRegedit();private:Ui::QSettingDemo *ui;
};
#endif // QSETTINGDEMO_H
#include "qsettingdemo.h"
#include "ui_qsettingdemo.h"
#include <QSettings>
#include <QDebug>
#include <QTextCodec>
#include <QIntValidator>QSettingDemo::QSettingDemo(QWidget *parent): QMainWindow(parent), ui(new Ui::QSettingDemo)
{ui->setupUi(this);initData();
}QSettingDemo::~QSettingDemo()
{delete ui;
}void QSettingDemo::initData()
{QValidator *validator = new QIntValidator(0, 9, ui->lineEdit);// the edit lineedit will only accept integers between 100 and 999ui->lineEdit->setValidator(validator);connect(ui->btnRead, SIGNAL(clicked()), this, SLOT(slotReadSetting()));connect(ui->btnWrite, SIGNAL(clicked()), this, SLOT(slotWriteSetting()));connect(ui->btnReadTitle, SIGNAL(clicked()), this, SLOT(slotTitleReadSetting()));connect(ui->btnWriteTitle, SIGNAL(clicked()), this, SLOT(slotTitleWriteSetting()));connect(ui->btnReadLayer, SIGNAL(clicked()), this, SLOT(slotLayerReadSetting()));connect(ui->btnWriteLayer, SIGNAL(clicked()), this, SLOT(slotLayerWriteSetting()));connect(ui->btnReadRegedit, SIGNAL(clicked()), this, SLOT(slotReadRegedit()));connect(ui->btnWriteRegedit, SIGNAL(clicked()), this, SLOT(slotWriteRegedit()));
}void QSettingDemo::slotReadSetting()
{QSettings settings("aSetting.ini", QSettings::IniFormat, this);QString name = settings.value("name", "").toString(); //默认值为空float height = settings.value("height", 1.6f).toFloat(); //默认值为1.6float weight = settings.value("weight", 18.6f).toFloat(); //默认值为18.6qDebug() << "slotReadSetting======name====" << name << " height=" << height << " weight=" << weight;
}void QSettingDemo::slotWriteSetting()
{QSettings settings("aSetting.ini", QSettings::IniFormat, this);settings.setValue("name", "Scott");settings.setValue("height", 174.6);settings.setValue("weight", 58.6);}void QSettingDemo::slotTitleReadSetting()
{QSettings settings("aSetting2.ini", QSettings::IniFormat, this);//settings.setIniCodec(QTextCodec::codecForName("UTF-8"));settings.beginGroup("string");QString name = settings.value("name", "").toString();//默认值为空QString sex = settings.value("sex", "").toString();//默认值为空QString nation = settings.value("nation", "汉族").toString();//默认值为"汉族"settings.endGroup();settings.beginGroup("data");int age = settings.value("age", 0).toInt();float height = settings.value("height", 0.0f).toFloat();float weight = settings.value("weight", 0.0f).toFloat();settings.endGroup();qDebug() << "slotTitleReadSetting======name====" << name << " sex=" << sex << " nation=" << nation;qDebug() << "slotTitleReadSetting======age=====" << age << " height=" << height << " weight=" << weight;
}void QSettingDemo::slotTitleWriteSetting()
{QSettings settings("aSetting2.ini", QSettings::IniFormat, this);settings.setIniCodec(QTextCodec::codecForName("GB2312"));settings.beginGroup("string");settings.setValue("name", QStringLiteral("何静"));settings.setValue("sex", QStringLiteral("男"));settings.setValue("nation", QStringLiteral("汉族"));settings.endGroup();settings.beginGroup("data");settings.setValue("age", 30);settings.setValue("height", 173.1);settings.setValue("weight", 62.2);settings.endGroup();
}void QSettingDemo::slotLayerReadSetting()
{QSettings settings("aSetting3.ini", QSettings::IniFormat, this);//settings.setIniCodec(QTextCodec::codecForName("UTF-8"));QString name = settings.value("string/name", "").toString();QString sex = settings.value("string/sex", "").toString();QString nation = settings.value("string/nation", "汉族").toString();int age = settings.value("data/age", 0).toInt();float height = settings.value("data/height", 0.0f).toFloat();float weight = settings.value("data/weight", 0.0f).toFloat();qDebug() << "slotLayerReadSetting======name====" << name << " sex=" << sex << " nation=" << nation;qDebug() << "slotLayerReadSetting======age=====" << age << " height=" << height << " weight=" << weight;
}void QSettingDemo::slotLayerWriteSetting()
{QSettings settings("aSetting3.ini", QSettings::IniFormat, this);settings.setIniCodec(QTextCodec::codecForName("GB2312"));settings.setValue("string/name", QStringLiteral("金玉清"));settings.setValue("string/sex", QStringLiteral("女"));settings.setValue("string/nation", QStringLiteral("朝鲜族"));settings.setValue("data/age", 33);settings.setValue("data/height", 163.1);settings.setValue("data/weight", 52.2);
}void QSettingDemo::slotReadRegedit()
{QSettings settings("Wumu Soft", "Clipper");QString version = settings.value("version").toString();qint64 lastTime = settings.value("lastTime").toLongLong();qDebug() << "version==============" << version << " lastTime=====" << lastTime;
}void QSettingDemo::slotWriteRegedit()
{QSettings settings("Wumu Soft", "Clipper");settings.setValue("version", "2.6.5");settings.setValue("lastTime", 1697190536);
}
这篇关于QSetting读写配置文件的基本使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!