summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorIvan Solovev <ivan.solovev@qt.io>2021-09-17 18:18:40 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2021-10-01 17:50:53 +0000
commit20ef130f3a55f24dfba09f3a3e49d73ab7e6869f (patch)
tree91a31d9eb3c3cfe401648be0e01c6c1b75de3ca0 /src
parent26bc8dd787f4a6e35cf319d518735b7922960088 (diff)
Let QLocale::uiLanguages() use WinRT API when possible
This patch introduces support for the WinRT UI languages API. We are using the Win32 API to get the list of preferred languages when the system locale is used. However, this API returns an incomplete list. As Qt 6 supports Windows 10 and above, we can make use of the WinRT API, if it's supported by the compiler. This API returns the full list, as reported by the Windows system itself. Note however, that this API can't be used with Clang and MinGW, so we still have to fall back to Win32 API for these compilers. We also do it if WinRT API returns an empty list of languages for some reason. Fixes: QTBUG-94341 Change-Id: I1d23c68d2ec298ae7835d0d18718876ff041aede Reviewed-by: Edward Welbourne <edward.welbourne@qt.io> (cherry picked from commit 51e8d3592acc8bacf326fe3933b5dec13bb518e6) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'src')
-rw-r--r--src/corelib/CMakeLists.txt7
-rw-r--r--src/corelib/text/qlocale_win.cpp27
2 files changed, 32 insertions, 2 deletions
diff --git a/src/corelib/CMakeLists.txt b/src/corelib/CMakeLists.txt
index 0bcc3b75b5..eda2c8dea7 100644
--- a/src/corelib/CMakeLists.txt
+++ b/src/corelib/CMakeLists.txt
@@ -780,11 +780,16 @@ qt_internal_extend_target(Core CONDITION UNIX AND (NACL OR NOT APPLE)
text/qlocale_unix.cpp
)
-qt_internal_extend_target(Core CONDITION WIN32 AND (NACL OR NOT APPLE)
+qt_internal_extend_target(Core CONDITION WIN32
SOURCES
text/qlocale_win.cpp
)
+qt_internal_extend_target(Core CONDITION WIN32 AND MSVC
+ LIBRARIES
+ runtimeobject
+)
+
qt_internal_extend_target(Core CONDITION QT_FEATURE_icu
SOURCES
text/qcollator_icu.cpp
diff --git a/src/corelib/text/qlocale_win.cpp b/src/corelib/text/qlocale_win.cpp
index 95279e278b..1278b73f02 100644
--- a/src/corelib/text/qlocale_win.cpp
+++ b/src/corelib/text/qlocale_win.cpp
@@ -51,6 +51,20 @@
# include <time.h>
#endif
+#if defined(Q_CC_MSVC)
+# include <winrt/base.h>
+// Workaround for Windows SDK bug.
+// See https://github.com/microsoft/Windows.UI.Composition-Win32-Samples/issues/47
+namespace winrt::impl
+{
+ template <typename Async>
+ auto wait_for(Async const& async, Windows::Foundation::TimeSpan const& timeout);
+}
+# include <winrt/Windows.Foundation.h>
+# include <winrt/Windows.Foundation.Collections.h>
+# include <winrt/Windows.System.UserProfile.h>
+#endif // defined(Q_CC_MSVC)
+
QT_BEGIN_NAMESPACE
static QByteArray getWinLocaleName(LCID id = LOCALE_USER_DEFAULT);
@@ -616,6 +630,18 @@ QVariant QSystemLocalePrivate::toCurrencyString(const QSystemLocale::CurrencyToS
QVariant QSystemLocalePrivate::uiLanguages()
{
+ QStringList result;
+#if defined(Q_CC_MSVC) // msvc supports WinRT calls
+ using namespace winrt;
+ using namespace Windows::Foundation;
+ using namespace Windows::System::UserProfile;
+ auto languages = GlobalizationPreferences::Languages();
+ for (const auto &lang : languages)
+ result << QString::fromStdString(winrt::to_string(lang));
+ if (!result.isEmpty())
+ return result; // else just fall back to WIN32 API implementation
+#endif // defined(Q_CC_MSVC)
+ // mingw and clang still have to use Win32 API
unsigned long cnt = 0;
QVarLengthArray<wchar_t, 64> buf(64);
# if !defined(QT_BOOTSTRAPPED) // Not present in MinGW 4.9/bootstrap builds.
@@ -630,7 +656,6 @@ QVariant QSystemLocalePrivate::uiLanguages()
}
}
# endif // !QT_BOOTSTRAPPED
- QStringList result;
result.reserve(cnt);
const wchar_t *str = buf.constData();
for (; cnt > 0; --cnt) {