From ba343fbbd4c71d96d178b7a5dd4279fdda82f6bc Mon Sep 17 00:00:00 2001 From: Yuhang Zhao <2546789017@qq.com> Date: Thu, 1 Sep 2022 16:45:32 +0800 Subject: [PATCH] win: fix bug when using Qt 5.14 and below Fixes: #152 Signed-off-by: Yuhang Zhao <2546789017@qq.com> --- .../Core/private/registrykey_p.h | 17 ++++++++++++++++- src/core/registrykey.cpp | 14 ++++++++------ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/include/FramelessHelper/Core/private/registrykey_p.h b/include/FramelessHelper/Core/private/registrykey_p.h index 34a2cbbc..8d4440a6 100644 --- a/include/FramelessHelper/Core/private/registrykey_p.h +++ b/include/FramelessHelper/Core/private/registrykey_p.h @@ -29,6 +29,21 @@ #include #include +#ifndef REGISTRYKEY_FORCE_QSETTINGS +# define REGISTRYKEY_FORCE_QSETTINGS (0) +#endif // REGISTRYKEY_FORCE_QSETTINGS + +#ifndef REGISTRYKEY_IMPL +# if ((QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) && !(REGISTRYKEY_FORCE_QSETTINGS)) +# define REGISTRYKEY_IMPL (1) +# else // ((QT_VERSION < QT_VERSION_CHECK(5, 14, 0)) || REGISTRYKEY_FORCE_QSETTINGS) +# define REGISTRYKEY_IMPL (2) +# endif // ((QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) && !REGISTRYKEY_FORCE_QSETTINGS) +#endif // REGISTRYKEY_IMPL + +#define REGISTRYKEY_QWINREGISTRYKEY ((REGISTRYKEY_IMPL) == 1) +#define REGISTRYKEY_QSETTINGS ((REGISTRYKEY_IMPL) == 2) + QT_BEGIN_NAMESPACE class QWinRegistryKey; class QSettings; @@ -64,7 +79,7 @@ class FRAMELESSHELPER_CORE_API RegistryKey : public QObject private: Global::RegistryRootKey m_rootKey = Global::RegistryRootKey::CurrentUser; QString m_subKey = {}; -#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) +#if REGISTRYKEY_QWINREGISTRYKEY QScopedPointer m_registryKey; #else QScopedPointer m_settings; diff --git a/src/core/registrykey.cpp b/src/core/registrykey.cpp index 574c906b..13d5275e 100644 --- a/src/core/registrykey.cpp +++ b/src/core/registrykey.cpp @@ -23,8 +23,9 @@ */ #include "registrykey_p.h" +#include "framelesshelper_windows.h" #include -#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) +#if REGISTRYKEY_QWINREGISTRYKEY # include #else # include @@ -76,15 +77,16 @@ RegistryKey::RegistryKey(const RegistryRootKey root, const QString &key, QObject } m_rootKey = root; m_subKey = key; -#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) +#if REGISTRYKEY_QWINREGISTRYKEY m_registryKey.reset(new QWinRegistryKey(g_keyMap[static_cast(m_rootKey)], m_subKey)); if (!m_registryKey->isValid()) { m_registryKey.reset(); } #else const QString rootKey = g_strMap[static_cast(m_rootKey)]; - m_settings.reset(new QSettings(rootKey, QSettings::NativeFormat)); - if (m_settings->contains(m_subKey)) { + const auto lastSlashPos = m_subKey.lastIndexOf(u'\\'); + m_settings.reset(new QSettings(rootKey + u'\\' + m_subKey.left(lastSlashPos), QSettings::NativeFormat)); + if (m_settings->childGroups().contains(m_subKey.mid(lastSlashPos + 1))) { m_settings.reset(new QSettings(rootKey + u'\\' + m_subKey, QSettings::NativeFormat)); } else { m_settings.reset(); @@ -106,7 +108,7 @@ QString RegistryKey::subKey() const bool RegistryKey::isValid() const { -#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) +#if REGISTRYKEY_QWINREGISTRYKEY return (!m_registryKey.isNull() && m_registryKey->isValid()); #else return !m_settings.isNull(); @@ -120,7 +122,7 @@ QVariant RegistryKey::value(const QString &name) const if (name.isEmpty() || !isValid()) { return {}; } -#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) +#if REGISTRYKEY_QWINREGISTRYKEY const QPair dwVal = m_registryKey->dwordValue(name); if (dwVal.second) { return qulonglong(dwVal.first);