diff --git a/src/corelib/kernel/qwinregistry.cpp b/src/corelib/kernel/qwinregistry.cpp index 48b553ae03..fe0cd62bdd 100644 --- a/src/corelib/kernel/qwinregistry.cpp +++ b/src/corelib/kernel/qwinregistry.cpp @@ -38,11 +38,12 @@ void QWinRegistryKey::close() QVariant QWinRegistryKey::value(QStringView subKey) const { - Q_ASSERT(!subKey.isEmpty()); - if (!isValid()) return {}; + if (subKey.isEmpty()) + subKey = u"Default"; + auto subKeyC = reinterpret_cast(subKey.utf16()); // Get the size and type of the value. diff --git a/tests/auto/corelib/kernel/qwinregistrykey/tst_qwinregistrykey.cpp b/tests/auto/corelib/kernel/qwinregistrykey/tst_qwinregistrykey.cpp index d3a20be048..81f75a7bea 100644 --- a/tests/auto/corelib/kernel/qwinregistrykey/tst_qwinregistrykey.cpp +++ b/tests/auto/corelib/kernel/qwinregistrykey/tst_qwinregistrykey.cpp @@ -20,6 +20,7 @@ static const QPair TEST_DWORD = qMakePair(u"dword", 123); static const QPair TEST_QWORD = qMakePair(u"qword", 456); static const QPair TEST_BINARY = qMakePair(u"binary", "binary\0"_ba); static const QPair TEST_NOT_EXIST = qMakePair(u"not_exist", QVariant()); +static const QPair TEST_DEFAULT = qMakePair(u"Default", u"default"_s); [[nodiscard]] static inline bool write(const HKEY key, const QStringView name, const QVariant &value) { @@ -124,6 +125,8 @@ void tst_qwinregistrykey::initTestCase() return; if (!write(key, TEST_BINARY.first, TEST_BINARY.second)) return; + if (!write(key, TEST_DEFAULT.first, TEST_DEFAULT.second)) + return; m_available = true; } @@ -141,9 +144,10 @@ void tst_qwinregistrykey::cleanupTestCase() RegDeleteValueW(key, C_STR(TEST_DWORD.first)); RegDeleteValueW(key, C_STR(TEST_QWORD.first)); RegDeleteValueW(key, C_STR(TEST_BINARY.first)); + RegDeleteValueW(key, C_STR(TEST_DEFAULT.first)); #undef C_STR - RegCloseKey(key); RegDeleteKeyW(HKEY_CURRENT_USER, TEST_KEY); + RegCloseKey(key); } void tst_qwinregistrykey::qwinregistrykey() @@ -206,6 +210,18 @@ void tst_qwinregistrykey::qwinregistrykey() QCOMPARE(value.value_or(QVariant()), QVariant()); } + { + const auto value = registry.value(TEST_DEFAULT.first); + QVERIFY(value.has_value()); + QCOMPARE(value.value_or(QString()), TEST_DEFAULT.second); + } + + { + const auto value = registry.value(L""); + QVERIFY(value.has_value()); + QCOMPARE(value.value_or(QString()), TEST_DEFAULT.second); + } + { const QString value = registry.stringValue(TEST_STRING.first); QVERIFY(!value.isEmpty());