QSettings

QSettings vs. XML parser

Welcome,

This article is a comparison between QSettings and an XML format serialization.

The goal is to save/restore application settings. Based on a Qt linked application, QSettings is a very handful solution.

Small example,

Create our object:

[code]#include <QSettings>
...
QSettings* _settings = nullptr;

_settings = new QSettings(PATH_TO_YOUR + "/app.ini", QSettings::IniFormat);[/code]

// delete object

[code]_settings->sync();
delete _settings; _settings = nullptr;[/code]

Adding a key/value:

[code]QString key;
QVariant value;

_settings->setValue(key, value);[/code]

Retrieve it as a QVariant:

[code]QVariant result = _settings->value(key/*, Default Value*/);[/code]

With these few lines of code, we could create quite as many settings keys with quite different types as QVariant gives us. (Fundamental types and class values)

My experience is the following:

QSettings

  • everything goes through QVariant
  • simple, easy to use
  • platform independent

XML serialization/deserialization

  • key typo's are out of question,
  • more complex data structures
  • fast save/reload
  • platform independent
  • 3rd party tools can read/write output (which could be a disadvantage ;)

Let's create a simple example structure for an XML serialization:

 

[code]...
struct SettingsUI
{
ctor
dtor
serialization
deserialization

QString _windowsPos;
QStringList _openedWindows;
QRect _windowRect;
...
};

...

struct SettingsDB
{
ctor
dtor
serialization
deserialization

unsigned _dbType;
int _dbPort;
QString _dbSchema;
...
};

struct SettingsMain
{
ctor
dtor
serialization
deserialization

...
SettingsUI _ui;
SettingsDB _db;
...
};
[/code]

 Using of one or another is mainly based on your use case. I hope this article helped you a bit.

 

Thanks for visiting.

The Q's are part of the Qt framework, all right reserved for Qt Company