修复在平台主题插件中错误的使用QApplication指针

This commit is contained in:
liuxinhao 2023-12-04 09:42:24 +08:00
parent 554b23c369
commit 459da492d3
2 changed files with 158 additions and 1 deletions

View File

@ -0,0 +1,153 @@
From 09c229bb81e3fc77c4d80ed309ae4135b1ff7410 Mon Sep 17 00:00:00 2001
From: liuxinhao <liuxinhao@kylinsec.com.cn>
Date: Fri, 1 Dec 2023 15:48:32 +0800
Subject: [PATCH] fix(theme): Fix the use of QApplication class for platform
theme plugin errors
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 修复在平台主题插件中错误的使用QApplication类
---
platformtheme/kiran-theme.cpp | 50 +++++++++++++++++++++++++----------
platformtheme/kiran-theme.h | 1 +
2 files changed, 37 insertions(+), 14 deletions(-)
diff --git a/platformtheme/kiran-theme.cpp b/platformtheme/kiran-theme.cpp
index 86eb2e1..c1ca81f 100644
--- a/platformtheme/kiran-theme.cpp
+++ b/platformtheme/kiran-theme.cpp
@@ -14,8 +14,8 @@
#include "kiran-theme.h"
#include "kiran-appearance-monitor.h"
-#include "logging-category.h"
#include "kiran-integration-settings.h"
+#include "logging-category.h"
#include <private/qguiapplication_p.h>
#include <private/qiconloader_p.h>
@@ -27,6 +27,8 @@
#undef private
#include <QApplication>
+#include <QGuiApplication>
+
#include <QIcon>
#include <QLabel>
#include <QPixmap>
@@ -38,6 +40,10 @@
using namespace Kiran;
+// NOTE: QPlatformTheme插件是QGuiApplication中init_platform将会进行加载的
+// 1. 关于Application的调用QPlatformTheme中只能使用QGuiApplication。
+// 2. 若一定需要使用QApplication的相关方法得先判断Qt应用代码中是否使用的是QApplication
+
KiranTheme::KiranTheme(const QStringList& paramList)
: QGenericUnixTheme()
{
@@ -86,7 +92,7 @@ QVariant KiranTheme::themeHint(QPlatformTheme::ThemeHint hint) const
<< "/usr/share/icons"
<< "/usr/local/share/icons";
case StyleNames:
- return QStringList{"kiran","fusion"};
+ return QStringList{"kiran", "fusion"};
default:
break;
}
@@ -101,7 +107,7 @@ const QPalette* KiranTheme::palette(QPlatformTheme::Palette type) const
}
bool enable = KiranIntegrationSettings::appKiranStyleAvailable(qAppName());
- if( !enable )
+ if (!enable)
{
return QGenericUnixTheme::palette(type);
}
@@ -119,9 +125,9 @@ void KiranTheme::init()
m_scaleFactor = m_settingsMonitor->scaleFactor();
qDebug(kiranPlatformTheme) << "\tscale factor:" << m_scaleFactor;
- m_systemFont.setFamily(m_settingsMonitor->appFont().family());
- m_systemFont.setPointSize(m_settingsMonitor->appFont().pointSize());
- QApplication::setFont(m_systemFont);
+ updateAppFont(m_settingsMonitor->appFont().family(),
+ m_settingsMonitor->appFont().pointSize());
+
qDebug(kiranPlatformTheme) << "\tapplication font:" << m_settingsMonitor->appFont().family() << m_settingsMonitor->appFont().pointSize();
m_titleBarFont.setFamily(m_settingsMonitor->titleBarFont().family());
@@ -135,14 +141,31 @@ void KiranTheme::init()
QObject::connect(m_settingsMonitor, &KiranAppearanceMonitor::cursorThemeChanged, this, &KiranTheme::handleCursorThemeChanged);
// 不从KiranAppearanceMonitor接受主题变更事件修改为接受KiranPalette的主题变更信号能监听到系统主题变更以及应用程序手动指定主题
- //QObject::connect(m_settingsMonitor, &KiranAppearanceMonitor::gtkThemeChanged, this, &KiranTheme::handleThemeChanged);
- QObject::connect(StylePalette::instance(),&StylePalette::themeChanged,this,&KiranTheme::handleThemeChanged);
+ // QObject::connect(m_settingsMonitor, &KiranAppearanceMonitor::gtkThemeChanged, this, &KiranTheme::handleThemeChanged);
+ QObject::connect(StylePalette::instance(), &StylePalette::themeChanged, this, &KiranTheme::handleThemeChanged);
- QObject::connect(qApp, &QApplication::screenAdded, this, &KiranTheme::handleScreenAdded);
+ QObject::connect(qApp, &QGuiApplication::screenAdded, this, &KiranTheme::handleScreenAdded);
handleScaleFactorChanged(m_scaleFactor);
}
+void KiranTheme::updateAppFont(const QString& fontFamily, int pointSize)
+{
+ m_systemFont.setFamily(fontFamily);
+ m_systemFont.setPointSize(pointSize);
+
+ auto coreApp = QGuiApplication::instance();
+ if (!qobject_cast<QApplication*>(coreApp))
+ {
+ // FIXME: 使用QGuiApplication更新字体不全
+ QGuiApplication::setFont(m_systemFont);
+ }
+ else
+ {
+ QApplication::setFont(m_systemFont);
+ }
+}
+
const QFont* KiranTheme::font(QPlatformTheme::Font type) const
{
switch (type)
@@ -177,9 +200,8 @@ void KiranTheme::handleAppFontChanged()
<< m_settingsMonitor->appFont().family()
<< m_settingsMonitor->appFont().pointSize();
- m_systemFont.setFamily(m_settingsMonitor->appFont().family());
- m_systemFont.setPointSize(m_settingsMonitor->appFont().pointSize());
- QApplication::setFont(m_systemFont);
+ updateAppFont(m_settingsMonitor->appFont().family(),
+ m_settingsMonitor->appFont().pointSize());
}
void KiranTheme::handleTitleBarFontChanged()
@@ -295,8 +317,8 @@ void KiranTheme::handleScaleFactorChanged(int factor)
void KiranTheme::handleCursorThemeChanged()
{
// 强制让窗口更新光标
- QApplication::setOverrideCursor(QCursor());
- QApplication::restoreOverrideCursor();
+ QGuiApplication::setOverrideCursor(QCursor());
+ QGuiApplication::restoreOverrideCursor();
}
bool KiranTheme::enableRealTimeScaling()
diff --git a/platformtheme/kiran-theme.h b/platformtheme/kiran-theme.h
index 243789c..4ef9c49 100644
--- a/platformtheme/kiran-theme.h
+++ b/platformtheme/kiran-theme.h
@@ -41,6 +41,7 @@ public:
private:
void init();
+ void updateAppFont(const QString& fontFamily,int pointSize);
static bool enableRealTimeScaling();
private slots:
--
2.33.0

View File

@ -1,6 +1,6 @@
Name: kiran-qt5-integration
Version: 2.4.0
Release: 13
Release: 14
Summary: Kiran desktop platform integration plugin.
License: MulanPSL-2.0
@ -16,6 +16,7 @@ Patch0007: 0007-fix-style-add-ksl-server-gui-and-ks-scmc-gui-to-kira.patch
Patch0008: 0008-fix-cmake-target-change-cmake-target-name.patch
Patch0009: 0009-fix-qt5.9.7-fits-the-Qt5.9.7-interface.patch
Patch0010: 0010-fix-style-add-ks-ssr-gui-to-white-list.patch
Patch0011: 0011-fix-theme-Fix-the-use-of-QApplication-class-for-plat.patch
BuildRequires: cmake >= 3.2
BuildRequires: gcc-c++
@ -71,6 +72,9 @@ make %{?_smp_mflags}
%{_libdir}/pkgconfig/kiran-style-helper.pc
%changelog
* Mon Dec 04 2023 liuxinhao <liuxinhao@kylinsec.com.cn> - 2.4.0-14
- KYOS-F: Fix the use of QApplication class for platform theme plugin errors
* Thu Nov 02 2023 liuxinhao <liuxinhao@kylinsec.com.cn> - 2.4.0-13
- KYOS-B: add ks-ssr-gui to white list