Check if a value registry exists with QSettings
Asked Answered
S

3

10

What i'm trying to do is to check if a registry key (NOT VALUE, KEY) exists in the registry. I can't find any way to check that.

Idea?

Slowpoke answered 13/12, 2011 at 5:11 Comment(0)
C
0

EDIT:

In 2011 I wrote:

The registry is a Windows concept and doesn't fit Qt's cross-platform notions. You will have to use the Windows API or a C++ wrapper for it.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms724875(v=vs.85).aspx

http://msdn.microsoft.com/en-us/library/xka57xy4(v=vs.80).aspx

If your needs are more abstract for your application to save and restore its own settings, Qt has a cross-platform design of something called QSettings.

Depending on the nature of the setting and the platform, will store these in the registry or in a file/etc.

But it appears in the answer by @mateuszb that QSettings can open Windows keys if you use QSettings::NativeFormat:

http://doc.qt.io/qt-5/qsettings.html#Format-enum

I'd still suggest that if you are hardcoding something like "HKEY_LOCAL_MACHINE" into your source, that you are not really in the spirit of abstracting your code across platforms in the way that Qt intends. But you apparently can (at least in recent Qt versions) do this without digging beneath Qt and calling the Windows registry APIs.

Callow answered 13/12, 2011 at 5:17 Comment(0)
E
21

Using QSettings you can open the key's parent and retrieve the list of its keys. Use the function childGroups() to get the list of keys. It seems that "groups" in qt are keys in Windows registry.

This is the only way I found to check whether a key exists. In this code I look for the key "SearchedKey".

QSettings settings(
    "HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\App Paths",
    QSettings::NativeFormat
);

if (settings.childGroups().contains("SearchedKey", Qt::CaseInsensitive))
    std::cout << "Key exists" << std::endl;
else
    std::cout << "Key doesn't exist" << std::endl;
Extirpate answered 24/2, 2012 at 18:57 Comment(1)
Note: If you're not using the special key "HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\App Paths", but something default like QSettings settings("HKEY_LOCAL_MACHINE\\Software\\MySoft\\Star Runner\\CurrentVersion\\App Paths", QSettings::NativeFormat);, then calling settings.childGroups() will create an empty registry key HKEY_LOCAL_MACHINE\SOFTWARE\MySoft\Star Runner\CurrentVersion\App Paths. This might be an undesired side effect. I believe that there is no way to prevent this from happening.Hardly
C
0

EDIT:

In 2011 I wrote:

The registry is a Windows concept and doesn't fit Qt's cross-platform notions. You will have to use the Windows API or a C++ wrapper for it.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms724875(v=vs.85).aspx

http://msdn.microsoft.com/en-us/library/xka57xy4(v=vs.80).aspx

If your needs are more abstract for your application to save and restore its own settings, Qt has a cross-platform design of something called QSettings.

Depending on the nature of the setting and the platform, will store these in the registry or in a file/etc.

But it appears in the answer by @mateuszb that QSettings can open Windows keys if you use QSettings::NativeFormat:

http://doc.qt.io/qt-5/qsettings.html#Format-enum

I'd still suggest that if you are hardcoding something like "HKEY_LOCAL_MACHINE" into your source, that you are not really in the spirit of abstracting your code across platforms in the way that Qt intends. But you apparently can (at least in recent Qt versions) do this without digging beneath Qt and calling the Windows registry APIs.

Callow answered 13/12, 2011 at 5:17 Comment(0)
O
0

There is still not a way that I can find for checking for groups. However you can set a key inside a group and check for the existence of that key:

QString groupname = "group";
QString keyname = "/name";
QString name_read = settings.value(groupname + keyname, QString()).toString();

if(name_read == groupname){
    ...
}else {
    // default action
}

This requires an additional key inside of the group called "name" that is set to the name of your group.

Ochoa answered 20/3, 2020 at 16:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.