Reading and writing operations of INI configuration files in Qt

int files should be familiar to most people. Generally, some configuration files in large-scale software use ini format files. Qt also provides functions such as creation of ini files, addition and deletion of elements, etc.

The data in ini generally contains groups, and the key and value values ​​contained in the group.

Qt uses the class QSettings, which implements functions related to ini format file operations.

Qt's official documentation states that there are two ways to use the QSettings class. One is to configure its properties into the system's registry through QSettings, and the other is to save it as a local ini file.

Use the QSettings class to save configurations to the registry

The usage steps are as follows:

1. Use QCoreApplication::setOrganizationName("Company Name"); and QCoreApplication::setApplicationName("Application Name");

2. Directly use QSettings to construct the object without parameters.

3. Use the setValue() function of QSettings to set the attribute value.

4. Use the value of QSettings to obtain the attribute value.

For example, if your product is called Star Runner and your company is called MySoft. (This is an example in qt)

QCoreApplication::setOrganizationName("MySoft");
QCoreApplication::setApplicationName("Star Runner");
    ...
QSettings settings;

The above is the first step. After this step is executed, the editor estimates that the corresponding registry structure MySoft–>Star Runner will be created in the registry.

You can then use the QSettings object to create and obtain software property values ​​anywhere in the project.

settings.setValue("editor/wrapMargin", 68);//设置属性值
int margin = settings.value("editor/wrapMargin").toInt();//获取属性值

If there are groups in QSettings, you can use the following method. For example, there is a sensor group, sensorType is the attribute, and camera is the attribute value.

Set the property value to:

//m_settings是QSettings的对象
m_settings->beginGroup("sensor"); //打开组,如果没有就创建一个
m_settings->setValue("sensorType","camera");//设置属性和值
m_settings->endGroup();//结束值设置

Get attribute value:

m_settings->beginGroup("sensor");//打开组
value = m_settings->value("sensorType").toString();//获取属性值
m_settings->endGroup();

At this point, the registry property settings are completed. What needs to be noted here is the difference in the registry in each system.

In addition, the properties set through the above method can be accessed directly through the registry. The above method is mainly suitable for setting global properties, and it is relatively convenient to use the above method.

Direct access to ini files

Directly using the ini local file is almost similar to writing to the registration standard operation process, except that it is different when creating the QSettings object.

1. Use the QSettings class to create an object. Two parameters (file storage path and file format) need to be introduced during the object creation process.

2. Use the setValue() function of QSettings to set the attribute value.

3. Use the value of QSettings to obtain the attribute value.

This usage method can facilitate the modification of the configuration file, but it cannot be used globally during the use of the software. However, the singleton mode of C++ can be used to achieve a globally unique usage method.

For example: Create a configuration file with the configuration file myapp.ini and a configuration file with the path /home/petra/misc/myapp.ini.

QSettings settings("/Users/petra/misc/myapp.plist",
                   QSettings::NativeFormat);//创建QSettings对象

You can then use the QSettings object to create and obtain software property values ​​anywhere in the project.

settings.setValue("editor/wrapMargin", 68);//设置属性值
int margin = settings.value("editor/wrapMargin").toInt();//获取属性值

If there are groups in QSettings, you can use the following method. For example, there is a sensor group, sensorType is the attribute, and camera is the attribute value.

Set the property value to:

//m_settings是QSettings的对象
m_settings->beginGroup("sensor"); //打开组,如果没有就创建一个
m_settings->setValue("sensorType","camera");//设置属性和值
m_settings->endGroup();//结束值设置

Get attribute value:

m_settings->beginGroup("sensor");//打开组
value = m_settings->value("sensorType").toString();//获取属性值
m_settings->endGroup();

At this point, the configuration file is set up.

The following is a singleton mode class encapsulated by the editor:

#ifndef CONFIGUREFILE_H
#define CONFIGUREFILE_H
#include <QString>
#include <QSettings>
class configureFile
{
    
    
public:
    /**
     * @brief writeConfig 向配置文件中添加数据
     * @param group 所要添加的分类
     * @param key 所要添加的键
     * @param value 所要添加的值
     */
    void writeConfig(QString group,QString key,QString value);

    /**
     * @brief readConfig 获取配置文件中对应键值的数据
     * @param group 分类
     * @param key 键
     * @return 值
     */
    QString readConfig(QString group,QString key);

    /**
     * @brief getInterface 获取配置文件对象
     * @return  返回配置文件对象
     */
    static configureFile* getInterface();

private:
    configureFile();
    ~configureFile();

    QSettings * m_settings = nullptr;
    static configureFile * m_configFile;
};

#endif // CONFIGUREFILE_H

#include "configurefile.h"

configureFile* configureFile::m_configFile = nullptr;
configureFile::configureFile()
{
    
       
    if(m_settings == nullptr){
    
    
         m_settings = new QSettings("./hwots.ini",QSettings::IniFormat);
    }
}
configureFile::~configureFile()
{
    
    
    if(m_settings != nullptr){
    
    
          delete m_settings;
    }
}

void configureFile::writeConfig(QString group, QString key, QString value)
{
    
    
    m_settings->beginGroup(group);
    m_settings->setValue(key,value);
    m_settings->endGroup();
}

QString configureFile::readConfig(QString group, QString key)
{
    
    
    QString value;
    m_settings->beginGroup(group);
    value = m_settings->value(key).toString();
    m_settings->endGroup();
    return value;
}

configureFile* configureFile::getInterface()
{
    
    
    if(m_configFile == nullptr){
    
    
        m_configFile = new configureFile();
    }
    return m_configFile;
}

Guess you like

Origin blog.csdn.net/qq_43812868/article/details/133377820