Qt读写配置文件之QSettings的用法

时间:2022-09-05 09:14:51

主要是在开发中,将一些关键的东西写入或者读取配置文件中,本文主要就是使用了QSettings的setValuevalue

QSettings可以存储一系列设置。每个设置包括指定设置名称(键)的一个字符串和一个与该键关联的QVariant存储数据。使用setValue()可以实现一个设置。

Qt的帮助文档下可以看到:

void QSettings::setValue(constQString & key, constQVariant & value)

Sets the value of setting key to value. If the key already exists, the previous value is overwritten.

Note that the Windows registry and INI files use case-insensitive keys, whereas the CFPreferences API on OS X and iOS uses case-sensitive keys. To avoid portability problems, see the Section and Key Syntax rules.
Example:    

QSettings settings;

 settings.setValue("interval", 30);
settings.value("interval").toInt();     // returns 30
settings.setValue("interval", 6.55);
settings.value("interval").toDouble();  // returns 6.55

QVariantQSettings::value(const QString& key, const QVariant& defaultValue = QVariant()) const
Returns the value for setting key. If the setting doesn't exist, returns defaultValue.


If no default value is specified, a default QVariant is returned.

Note that the Windows registry and INI files use case-insensitive keys, whereas the CFPreferences API on OS X and iOS uses case-sensitive keys. To avoid portability problems, see the Section and Key Syntax rules.

Example:

QSettings settings;
settings.setValue("animal/snake", 58);
settings.value("animal/snake", 1024).toInt();   // returns 58
settings.value("animal/zebra", 1024).toInt();   // returns 1024
settings.value("animal/zebra").toInt();         // returns 0

上面的都是Qt帮助文档中的:

具体实例如下:

config.h

#ifndef CONFIG_H
#define CONFIG_H

#include <QVariant>
#include <QSettings>

class Config
{
public:
    Config(QString qstrfilename = "");
    virtual ~Config(void);
    void Set(QString,QString,QVariant);
    QVariant Get(QString,QString);
private:
    QString m_qstrFileName;
    QSettings *m_psetting;
};

#endif // CONFIG_H
config.cpp

#include "config.h"
#include <QtCore/QtCore>
#include <QDebug>

Config::Config(QString qstrfilename)
{
    if (qstrfilename.isEmpty())
    {
        m_qstrFileName = QCoreApplication::applicationDirPath() + "/Config.ini";
    }
    else
    {
        m_qstrFileName = qstrfilename;
    }

    m_psetting = new QSettings(m_qstrFileName, QSettings::IniFormat);
    qDebug() << m_qstrFileName;
}

Config::~Config()
{
    delete m_psetting;
    m_psetting = 0;
}

void Config::Set(QString qstrnodename,QString qstrkeyname,QVariant qvarvalue)
{
    m_psetting->setValue(QString("/%1/%2").arg(qstrnodename).arg(qstrkeyname), qvarvalue);
}

QVariant Config::Get(QString qstrnodename,QString qstrkeyname)
{
    QVariant qvar = m_psetting->value(QString("/%1/%2").arg(qstrnodename).arg(qstrkeyname));
    return qvar;
}

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include "config.h"
#include <QDebug>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    QString qstrname = Config().Get("user","name").toString();
    qDebug() << qstrname;
    QString qstrpasswd = Config().Get("user","password").toString();
    qDebug() << qstrpasswd;
    int nport = Config().Get("test","port").toInt();
    qDebug() << nport;
}

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

主要,配置文件需要放置在debug目录下,具体可以看debug的输出目录,如下图所示:

Qt读写配置文件之QSettings的用法

上面是Config.ini的路径。

下面是Config.ini配置文件的具体内容:

Qt读写配置文件之QSettings的用法