QSettings read and write ini files

Introduction to QSettings

The QSettings class provides persistent platform-independent application settings.
Users often expect an application to remember its settings (window size and position, options, etc.) across sessions. On Windows, this information is typically stored in the system registry, and on macOS and iOS in property list files. On Unix systems, in the absence of a standard, many applications (including KDE applications) use INI text files.
QSettings is an abstraction around these technologies that enables you to save and restore application settings in a portable manner. It also supports custom storage formats.
QSettings' API is based on QVariant and allows you to save most value-based types such as QString, QRect and QImage with minimal effort.
If all you need is a non-persistent memory based structure, consider using QMap<QString, QVariant>.

Common APIs

Constructs a QSettings object for accessing settings stored in a file named fileName, with a parent element. Create the file if it doesn't already exist. If format is QSettings::NativeFormat, the meaning of fileName depends on the platform. On Unix, fileName is the name of an INI file. On macOS and iOS, fileName is the name of a .plist file. On Windows, fileName is a path in the system registry.
If format is QSettings::IniFormat, fileName is the name of the INI file.

QSettings::QSettings(const QString &fileName, QSettings::Format format, QObject *parent = nullptr)

Append the prefix to the current group. It can also be understood as starting a section or key-value pair.

void QSettings::beginGroup(const QString &prefix)

Used in conjunction with QSettings::beginGroup.

void QSettings::endGroup()

Set the value of the set key to value. If the key already exists, overwrite the previous value.

void QSettings::setValue(const QString &key, const QVariant &value)

Returns the value of the set key. If the setting does not exist, defaultValue is returned.
If no default value is specified, the default QVariant is returned.

QVariant QSettings::value(const QString &key, const QVariant &defaultValue = QVariant()) const

Returns true if a setting named key exists; otherwise returns false.

bool QSettings::包含(const QString &key) const

insert image description here
ini
insert image description here
file.h file

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
    
    
class Widget;
}

class Widget : public QWidget
{
    
    
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

    void readIni();
    void writeIni();
protected:
     void closeEvent(QCloseEvent *event) override;

private slots:
     void on_btnSetColor_clicked();

private:
    Ui::Widget *ui;
};

#endif // WIDGET_H

.cpp ask price

#include "widget.h"
#include "ui_widget.h"
#include<QSettings>
#include<QCloseEvent>
#include<QColorDialog>
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    
    
    ui->setupUi(this);

    readIni();

}

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

void Widget::readIni()
{
    
    
    QSettings settings("GuiState.ini",QSettings::IniFormat);

    settings.beginGroup("MainWindow");
    resize(settings.value("size", QSize(400, 400)).toSize());
    move(settings.value("pos", QPoint(200, 200)).toPoint());

    //设置背景
    QPalette pal(this->palette());
    pal.setColor(QPalette::Background, settings.value("color", QColor(255,255,255)).value<QColor>());
    this->setAutoFillBackground(true);
    this->setPalette(pal);

    settings.endGroup();
}

void Widget::writeIni()
{
    
    
    QSettings settings("GuiState.ini",QSettings::IniFormat);

    settings.beginGroup("MainWindow");
    settings.setValue("size", size());
    settings.setValue("pos", pos());
    settings.endGroup();
}

void Widget::closeEvent(QCloseEvent *event)
{
    
    
    writeIni();
}

void Widget::on_btnSetColor_clicked()
{
    
    
    QColorDialog colorDialog;
    connect(&colorDialog, &QColorDialog::colorSelected, this, [&](QColor curColor){
    
    

        QSettings settings("GuiState.ini",QSettings::IniFormat);
        settings.beginGroup("MainWindow");
        settings.setValue("color", curColor);
        settings.endGroup();

        //设置背景
        QPalette pal(this->palette());
        pal.setColor(QPalette::Background, curColor);
        this->setAutoFillBackground(true);
        this->setPalette(pal);

    });
    colorDialog.exec();

}

Guess you like

Origin blog.csdn.net/weixin_44901043/article/details/124270126