Application settings using QSettings

Introduction

Generally the user expects the application to keep the settings across the sessions. For example, setting up the application's window size, window colour, theme, position, various application specific options and etc,. This settings will be stored generally in registry, but some times in INI file on Windows OS depending upon the application developer, property list on macOS, INI file on unix/linux.The main advantages is to customize the application at some extend based on user preferences and reduce keying the same data in each session.

QSettings Class

Qt provides an elegant solution for achieving this concept using QSettings class. QSettings is an abstraction around these technologies, enabling you to save and restore application settings in a portable manner. It also supports custom storage formats. This class is derived from QObject class.

Create Instance for QSettings

You can create the QSettings instance either in heap or stack. You've to pass two parameters like name of your company or organization and the name of your application, when creating instance for QSettings class.

For Example:

QSettings *settings = new QSettings("QtCompany", "My App");

or

QSettings settings("QtCompany", "My App");


This will read/write the folders in the registry using Company name and the application name. Another way is to read/write the INI file. The setting can be stored in the INI file instead of storing the settings into the registry with simple changes. For example,

QString file = QApplication::applicationDirPath()+"/setting.ini";
QSettings settings(file, QSettings::IniFormat);

Usage

QSettings provides the method called setValue and it is used to set the Key and Value combination in the registry. To write a settings, use setValue() method. For example, 


setting->setValue("Name", ui->txtName->text());


Here the "Name" is the property (Key) and the next parameter is the value of the Key. If the specified Key is already exists then the existing value is overwritten by the new value. There is a method called "value", to retrieve the value like this,

ui->txtName->setText(setting->value("Name").toString());


Some more Functions

The below table is giving you some more function and its description about the function to utilize the QSettings class effectively.

FunctionDescription
contains()To test whether a given key exists
remove()To remove the setting associated with a key
clear()To remove all keys
allKeys()To obtain the list of all keys
beginGroup()save or restore many settings with the same prefix (start point)
endGroup()save or restore many settings with the same prefix (end point)


mainwindow.cpp


#include "mainwindow.h"

#include "ui_mainwindow.h"

#include <QSettings>
 

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    connect(ui->btnSave, SIGNAL(clicked()), this, SLOT(writeData()));
    setting = new QSettings("QtCompany", "My App");
 
    // comment below line and check the output for the clear understanding
    readData(); 
}

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

void MainWindow::writeData() {

    setting->beginGroup("MainWindow");
    setting->setValue("Name", ui->txtName->text());
    setting->setValue("Age", ui->txtAge->text().toInt());
    setting->setValue("Address", ui->txtAddress->toPlainText());
    setting->setValue("Gender", ui->rdMale->isChecked());

    setting->setValue("size", size());
    setting->setValue("pos", pos());
    setting->endGroup();
}

void MainWindow::readData() {

    setting->beginGroup("MainWindow");
    ui->txtName->setText(setting->value("Name").toString());
    ui->txtAge->setText(setting->value("Age").toString());
    ui->txtAddress->setPlainText(setting->value("Address").toString());

    bool gender = setting->value("Gender").toBool();
    ui->rdMale->setChecked(gender);
    ui->rdFemale->setChecked(!gender);

    resize(setting->value("size", QSize(400, 400)).toSize());
    move(setting->value("pos", QPoint(200, 200)).toPoint());
    setting->endGroup();
}
 

mainwindow.h

 

#ifndef MAINWINDOW_H
#define MAINWINDOW_H


#include <QMainWindow>
#include <QSettings>


namespace Ui {
     class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
 
private:
    Ui::MainWindow *ui;
    QSettings *setting;

    void readData();
 
private slots:
    void writeData();
};

#endif // MAINWINDOW_H 
 
You can see the registry changes in the below image after running the above problem.
 
 


That's it!!! Hope I covered all the basic concepts and I hope, this will help you lot.