Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having hierarchical groups in ini file for QSettings

Tags:

qt

ini

I am using QSettings to make changes in my GUI. Currently I have understood how to use QSetting for basic key=value pairs.

[button]
  enable = 1

But I want to have hierarchical groups. Something like below

[user1]
  [button1]
     enable = 1
  [button2]
    enable = 0
[user2]
  [button1]
    enable = 1
  [button2]
    enable = 0

Is there any way to do this?

Thank You :)

like image 552
Sid411 Avatar asked Nov 12 '22 21:11

Sid411


1 Answers

Like @Tab and @vahancho pointed out, the Qt Docs say the following about this:

You can form hierarchical keys using the '/' character as a separator, similar to Unix file paths. For example:

settings.setValue("mainwindow/size", win->size());
settings.setValue("mainwindow/fullScreen", win->isFullScreen());
settings.setValue("outputpanel/visible", panel->isVisible());

While not stated explicitly in the docs, deeper hierarchies (e.g., mainwindow/titleBar/color) are supported. When persisting a QSettings with the format set to QSettings::IniFormat to a *.ini file on disk using sync, the top-level part of each hierarchical key (e.g., mainwindow) is mapped to an Ini Section. Because the ini file format doesn't support nested sections, the rest of the key remains untouched and becomes the key inside the ini section. This can be seen in QConfFileSettingsPrivate::writeIniFile:

    if ((slashPos = key.indexOf(QLatin1Char('/'))) != -1) {
        section = key.left(slashPos);
        key.remove(0, slashPos + 1);
    }

    QSettingsIniSection &iniSection = iniMap[section];
    iniSection.keyMap[key] = j.value();

Thus, a setting like settings.setValue("mainwindow/titleBar/color", "red"); becomes

[mainwindow]
titleBar/color = red

in the ini file.

like image 139
Mitja Avatar answered Nov 15 '22 11:11

Mitja