1 From add92a551cf601b5c9e074046326f95ccc38062e Mon Sep 17 00:00:00 2001
2 From: Albert Astals Cid <aacid@kde.org>
3 Date: Sat, 23 May 2020 01:35:18 +0200
4 Subject: [PATCH] Do not fully initialize QIconLoader when setting the fallback
5 theme
6
7 We need this because without this patch you get bugs both
8 if you call QIcon::setFallbackThemeName before creating the QGuiApplication and
9 if you call QIcon::setFallbackThemeName after creating QGuiApplication
10
11 Why do you get a bug if you call QIconLoader::setFallbackThemeName
12 before creating the QGuiApplication:
13 * QIcon::setFallbackThemeName calls QIconLoader::instance
14 * QIconLoader::instance calls QIconLoader::ensureInitialized
15 * QIconLoader::ensureInitialized calls systemThemeName
16 * systemThemeName asks the current QPlatformTheme for its
17 QPlatformTheme::SystemIconThemeName
18 * But since we're calling this before creating the QGuiApplication
19 there is no current QPlatformTheme yet, so systemThemeName
20 is set to empty, which is obviously not what we want
21
22 Why do you get a bug if you call QIconLoader::setFallbackThemeName
23 after creating the QGuiApplication:
24 * QGuiApplicationPrivate::init calls
25 QGuiApplicationPrivate::createPlatformIntegration
26 * QGuiApplicationPrivate::createPlatformIntegration sets the
27 current QPlatformTheme and at the end of the very same function
28 uses QIcon::fromTheme
29 * Since we haven't called QIconLoader::setFallbackThemeName yet
30 there is at least one icon lookup that doesn't take
31 the fallback theme we would like to have into account
32
33 This patch makes it so calling QIconLoader::setFallbackThemeName
34 before creating the QGuiApplication works.
35
36 The only thing we want to do from QIcon::setFallbackThemeName is set
37 the internal m_userFallbackTheme, it doesn't care about doing
38 further initialization of QIconLoader, if it's done, great it's done,
39 if it is not initialized yet, great it will be initialized later
40 when someone actually tries to use the QIconloader.
41
42 So it's OK for ensureInitialized() to return early if there is no
43 platform theme yet, because it will be called again later.
44
45 Pick-to: 5.12
46 Pick-to: 5.15
47 Fixes: QTBUG-74252
48 Change-Id: I65268fc3d3d0bd282d76c76cf75e495bcc9d1a30
49 Done-with: Albert Astals Cid <albert.astals.cid@kdab.com>
50 Reviewed-by: Albert Astals Cid <albert.astals.cid@kdab.com>
51 Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
52 ---
53 src/gui/image/qicon.cpp | 3 +++
54 src/gui/image/qiconloader.cpp | 15 ++++++++++++---
55 2 files changed, 15 insertions(+), 3 deletions(-)
56
57 diff --git a/src/gui/image/qicon.cpp b/src/gui/image/qicon.cpp
58 index 36f499711e9..91da21c477d 100644
59 --- a/src/gui/image/qicon.cpp
60 +++ b/src/gui/image/qicon.cpp
61 @@ -1238,6 +1238,9 @@ QString QIcon::fallbackThemeName()
62 themeSearchPath() containing an index.theme
63 file describing its contents.
64
65 + \note This should be done before creating \l QGuiApplication, to ensure
66 + correct initialization.
67 +
68 \sa fallbackThemeName(), themeSearchPaths(), themeName()
69 */
70 void QIcon::setFallbackThemeName(const QString &name)
71 diff --git a/src/gui/image/qiconloader.cpp b/src/gui/image/qiconloader.cpp
72 index 15ab1b3cd90..3fa3bb9c598 100644
73 --- a/src/gui/image/qiconloader.cpp
74 +++ b/src/gui/image/qiconloader.cpp
75 @@ -112,10 +112,9 @@ extern QFactoryLoader *qt_iconEngineFactoryLoader(); // qicon.cpp
76 void QIconLoader::ensureInitialized()
77 {
78 if (!m_initialized) {
79 + if (!QGuiApplicationPrivate::platformTheme())
80 + return; // it's too early: try again later (QTBUG-74252)
81 m_initialized = true;
82 -
83 - Q_ASSERT(qApp);
84 -
85 m_systemTheme = systemThemeName();
86
87 if (m_systemTheme.isEmpty())
88 @@ -125,6 +124,16 @@ void QIconLoader::ensureInitialized()
89 }
90 }
91
92 +/*!
93 + \internal
94 + Gets an instance.
95 +
96 + \l QIcon::setFallbackThemeName() should be called before QGuiApplication is
97 + created, to avoid a race condition (QTBUG-74252). When this function is
98 + called from there, ensureInitialized() does not succeed because there
99 + is no QPlatformTheme yet, so systemThemeName() is empty, and we don't want
100 + m_systemTheme to get intialized to the fallback theme instead of the normal one.
101 +*/
102 QIconLoader *QIconLoader::instance()
103 {
104 iconLoaderInstance()->ensureInitialized();
105 --
106 2.16.3
|