QSettings 用反斜杠替换斜杠(注册表)

QSettings replaces slashes with backslashes (registry)

我用 QSettings 读取注册表。我要阅读的关键是:

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

由于键名Source OS (Updated on 8/1/2015 02:45:41)是动态的,我直接打开

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

和 运行 通过 settings.childGroups() 递归并用这个构建我的密钥:

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

但这会导致:

\HKEY_LOCAL_MACHINE\SYSTEM\Setup\Source OS (Updated on 815 02:45:41)

如您所见,斜杠现在是反斜杠,我无法区分名称和路径部分。

知道为什么 QSettings 将斜杠转换为反斜杠吗?

来自 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.

通常,动态注册表项名称的想法不太好。我认为更好的方法是在您的密钥中创建两个单独的值 – Source OSUpdated On 值。