Skip to content
This repository has been archived by the owner on Dec 19, 2023. It is now read-only.

Commit

Permalink
win: fix bug when using Qt 5.14 and below
Browse files Browse the repository at this point in the history
Fixes: #152

Signed-off-by: Yuhang Zhao <[email protected]>
  • Loading branch information
wangwenx190 committed Sep 1, 2022
1 parent 6b1a37a commit ba343fb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
17 changes: 16 additions & 1 deletion include/FramelessHelper/Core/private/registrykey_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@
#include <QtCore/qvariant.h>
#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
class QWinRegistryKey;
class QSettings;
Expand Down Expand Up @@ -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<QWinRegistryKey> m_registryKey;
#else
QScopedPointer<QSettings> m_settings;
Expand Down
14 changes: 8 additions & 6 deletions src/core/registrykey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
*/

#include "registrykey_p.h"
#include "framelesshelper_windows.h"
#include <QtCore/qdebug.h>
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
#if REGISTRYKEY_QWINREGISTRYKEY
# include <QtCore/private/qwinregistry_p.h>
#else
# include <QtCore/qsettings.h>
Expand Down Expand Up @@ -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<int>(m_rootKey)], m_subKey));
if (!m_registryKey->isValid()) {
m_registryKey.reset();
}
#else
const QString rootKey = g_strMap[static_cast<int>(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();
Expand All @@ -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();
Expand All @@ -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<DWORD, bool> dwVal = m_registryKey->dwordValue(name);
if (dwVal.second) {
return qulonglong(dwVal.first);
Expand Down

0 comments on commit ba343fb

Please sign in to comment.