win: fix bug when using Qt 5.14 and below

Fixes: #152

Signed-off-by: Yuhang Zhao <2546789017@qq.com>
This commit is contained in:
Yuhang Zhao 2022-09-01 16:45:32 +08:00
parent 6b1a37a82a
commit ba343fbbd4
2 changed files with 24 additions and 7 deletions

View File

@ -29,6 +29,21 @@
#include <QtCore/qvariant.h> #include <QtCore/qvariant.h>
#include <optional> #include <optional>
#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 QT_BEGIN_NAMESPACE
class QWinRegistryKey; class QWinRegistryKey;
class QSettings; class QSettings;
@ -64,7 +79,7 @@ public:
private: private:
Global::RegistryRootKey m_rootKey = Global::RegistryRootKey::CurrentUser; Global::RegistryRootKey m_rootKey = Global::RegistryRootKey::CurrentUser;
QString m_subKey = {}; QString m_subKey = {};
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) #if REGISTRYKEY_QWINREGISTRYKEY
QScopedPointer<QWinRegistryKey> m_registryKey; QScopedPointer<QWinRegistryKey> m_registryKey;
#else #else
QScopedPointer<QSettings> m_settings; QScopedPointer<QSettings> m_settings;

View File

@ -23,8 +23,9 @@
*/ */
#include "registrykey_p.h" #include "registrykey_p.h"
#include "framelesshelper_windows.h"
#include <QtCore/qdebug.h> #include <QtCore/qdebug.h>
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) #if REGISTRYKEY_QWINREGISTRYKEY
# include <QtCore/private/qwinregistry_p.h> # include <QtCore/private/qwinregistry_p.h>
#else #else
# include <QtCore/qsettings.h> # include <QtCore/qsettings.h>
@ -76,15 +77,16 @@ RegistryKey::RegistryKey(const RegistryRootKey root, const QString &key, QObject
} }
m_rootKey = root; m_rootKey = root;
m_subKey = key; m_subKey = key;
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) #if REGISTRYKEY_QWINREGISTRYKEY
m_registryKey.reset(new QWinRegistryKey(g_keyMap[static_cast<int>(m_rootKey)], m_subKey)); m_registryKey.reset(new QWinRegistryKey(g_keyMap[static_cast<int>(m_rootKey)], m_subKey));
if (!m_registryKey->isValid()) { if (!m_registryKey->isValid()) {
m_registryKey.reset(); m_registryKey.reset();
} }
#else #else
const QString rootKey = g_strMap[static_cast<int>(m_rootKey)]; const QString rootKey = g_strMap[static_cast<int>(m_rootKey)];
m_settings.reset(new QSettings(rootKey, QSettings::NativeFormat)); const auto lastSlashPos = m_subKey.lastIndexOf(u'\\');
if (m_settings->contains(m_subKey)) { 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)); m_settings.reset(new QSettings(rootKey + u'\\' + m_subKey, QSettings::NativeFormat));
} else { } else {
m_settings.reset(); m_settings.reset();
@ -106,7 +108,7 @@ QString RegistryKey::subKey() const
bool RegistryKey::isValid() const bool RegistryKey::isValid() const
{ {
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) #if REGISTRYKEY_QWINREGISTRYKEY
return (!m_registryKey.isNull() && m_registryKey->isValid()); return (!m_registryKey.isNull() && m_registryKey->isValid());
#else #else
return !m_settings.isNull(); return !m_settings.isNull();
@ -120,7 +122,7 @@ QVariant RegistryKey::value(const QString &name) const
if (name.isEmpty() || !isValid()) { if (name.isEmpty() || !isValid()) {
return {}; return {};
} }
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) #if REGISTRYKEY_QWINREGISTRYKEY
const QPair<DWORD, bool> dwVal = m_registryKey->dwordValue(name); const QPair<DWORD, bool> dwVal = m_registryKey->dwordValue(name);
if (dwVal.second) { if (dwVal.second) {
return qulonglong(dwVal.first); return qulonglong(dwVal.first);