Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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;

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top