Explanation of QSettings and How to use in QT C++

Explanation of QSettings and How to use in QT C++

Hello everyone, in this article we are gong to talk about QSettings class and its usage in QT C++. Here we will use QSettings to store some informations as platform independent.

Let's begin.

Firstly What is QSettings and its purpose
QSettings class provides a technic to store user datas in a portable file with custom storage formats. QSettings does not use operating system specific ways and it works platform independent.

Now take a look at the usage of QSettings with examples.

Do not forget to include QSetting header in you class.


#include <QSettings>
Firstly we need to set application and organisation informations in our application to use QSettings. There are two ways to perform it:
  • You can initialize QSettings class with these informations or
  • You can set these informations with QCoreApplication class. I recommend run it in main function of program.

Below code block you can see passing the Application and Organisation informations.


//First way
QSettings settings_withAppInfo("Thecodeprogram", "QSettings Example Application");

//Second ways
QCoreApplication::setOrganizationName("Thecodeprogram");
QCoreApplication::setOrganizationDomain("Thecodeprogram.com");
QCoreApplication::setApplicationName("QSettings Example Application");

Now we need to initialize a QSettings class with some specifications. We can set the scope and FileFormat of QSettings at initialization.


QSettings settings1(QSettings::SystemScope);
QSettings settings2("thesettings.ini", QSettings::IniFormat);
QSettings settings3(QSettings::Registry64Format, QSettings::UserScope, "Thecodeprogram", "QSettings Example Application");
QSettings settings4("Thecodeprogram", "QSettings Example Application");
  • 1. We just specified Settings files' scope.
  • 2. We specified a specific file name and Settings Format as Ini store format.
  • 3. We specified format and Application information of QSettings.
  • 4. We specified application and organisation information.

Now we are ready to store some datas in our settings file.

Below code block you can see the storing of the general data and grouped data.


//General Data
settings2.setValue("author", "Burak Hamdi TUFAN");
settings2.setValue("web", "thecodeprogram.com");
//Grouped data
settings2.setValue("Lang/programming", "C++");
settings2.setValue("Lang/framework", "Qt Framework");
settings2.setValue("Lang/experience", "6");

cout << "Author : " << settings2.value("author").toByteArray().constData() << endl;
cout << "Web : " << settings2.value("web").toByteArray().constData() << endl;
cout << "Programming Lang : " << settings2.value("Lang/programming").toByteArray().constData() << endl;
cout << "Framework : " << settings2.value("Lang/framework").toByteArray().constData() << endl;
cout << "Experience : " << settings2.value("Lang/experience").toByteArray().constData() << " years" << endl;
Below image you can see the content of ini file and output of above example. QSettings Ini file content and writing example output

We can also store in settings file QVariant data types. This means we can save QColor, QSize, QPalette.. all data types that QVariant can accept. All we need to store datas and read them with casting related data type to use it in program.

We can also get grouplist and key list of settings file. Below code block you can see it


cout << endl << "------- ALL KEYS --------" << endl;

QStringList keys = settings2.allKeys();
for(int i=0; i<keys.size(); i++)
    cout << "Key : " << i << " / Value : " << keys.at(i).toUtf8().constData() << endl;

cout<< endl << "------- GROUPS and SUBKEYS --------" << endl ;

QStringList groups = settings2.childGroups();
for(int i=0; i<groups.size(); i++){
    cout << "Group : " << i << " = : " << groups.at(i).toUtf8().constData() << endl;
    settings2.beginGroup(groups.at(i).toUtf8().constData());
    QStringList subKeyList = settings2.childKeys();
    for(int j=0; j<subKeyList.size(); j++)
        cout << "--SubKey : " << j << " = : " << subKeyList.at(j).toUtf8().constData() << endl;

    settings2.endGroup();
}
Below image you can see the output of reading keys and groups. Reading Keys and groups of QSettings Example output

So, some times we may need to store our datas inside a Custom class. In these cases we need to write these custom classes inside our settings file. In here we need to perform some extra operations. We are going to overload operators and we will use related datas as DataStream.

Do not forget to include required headers:

#include <QMetaType>
#include <QVariant>
#include <QDataStream>

Below code block you can see Custom class decleration and overloading operations.


struct UserCLass {
    QString name;
    QString surname;
    QString web;
    int experience;
};

//Declare the user defined class as METATYPE for Meta Object Compiler
Q_DECLARE_METATYPE(UserCLass);

//We need to overload in and out operators
QDataStream& operator<<(QDataStream& dataOut, const UserCLass& u) {
    dataOut << u.name << u.surname << u.web << u.experience;
    return dataOut;
}

QDataStream& operator>>(QDataStream& dataIn, UserCLass& u) {
    dataIn >> u.name;
    dataIn >> u.surname;
    dataIn >> u.web;
    dataIn >> u.experience;
    return dataIn;
}

And below code block in related event handler how can we handle above custom class with settings file.


    cout<< endl << "------- CUSTOM CLASS --------" << endl ;
    qRegisterMetaTypeStreamOperators<UserCLass>("UserCLass");

    UserCLass user;
    user.name = "Burak Hamdi";
    user.surname = "TUFAN";
    user.web = "thecodeprogram.com";
    user.experience = 6;
    settings3.setValue("user/userData", QVariant::fromValue(user));

    QVariant storedData = settings3.value("user/userData");
    UserCLass storedUser = storedData.value<UserCLass>();

    cout<< "Name : " << storedUser.name.toUtf8().constData() << endl;
    cout<< "Surname : " << storedUser.surname.toUtf8().constData() << endl ;
    cout<< "Web : " << storedUser.web.toUtf8().constData() << endl ;
    cout<< "Experience : " << QString::number(storedUser.experience).toUtf8().constData() << " years" << endl ;

In here we need to be carefull to register your QVariant class to Meta Object Compiler with qRegisterMetaTypeStreamOperators method. Otherwise MOC will not recognise your class.

Below image you can see settings file content and custom class usage example output. Custom Class with QSettings example output and file content

Now you are good to use QSettings file in your QT C++ projects.

That is all in this article.

You can reach the example project on Github via below link: https://github.com/thecodeprogram/QSettingsExample

Burak Hamdi TUFAN


Tags


Share this Post

Send with Whatsapp

Post a Comment

Success! Your comment sent to post. It will be showed after confirmation.
Error! There was an error sending your comment.

Comments

  • There is no comment. Be the owner of first comment...