windowsqtregistryqsettings

QSettings replaces slashes with backslashes (registry)


I read the registry with QSettings. The key I want to read is this:

HKEY_LOCAL_MACHINE\SYSTEM\Setup\Source OS (Updated on 8/1/2015 02:45:41)

Since the key name Source OS (Updated on 8/1/2015 02:45:41) is dynamic, I just open

QSettings settings("HKEY_LOCAL_MACHINE\\SYSTEM\\Setup", QSettings::NativeFormat);

and run recursive through the settings.childGroups() and build my key with this:

QString key = settings.fileName() + "\\" + settings.group();

But this results in:

\HKEY_LOCAL_MACHINE\SYSTEM\Setup\Source OS (Updated on 8\1\2015 02:45:41)

As you can see the slashes are now backslashes and I cannot distinguish between name and path pieces.

Any idea why QSettings converts the slashes into backslashes?


Solution

  • From the QSettings documentation:

    Do not use slashes ('/' and '\') in section or key names; the backslash character is used to separate sub keys (see below). On windows '\' are converted by QSettings to '/', which makes them identical.

    Generally, the idea of dynamic registry key names is not so good. I think the better approach is to create two separate values inside your key – Source OS and Updated On values.