From 9f6600de3b22f948c9913e913d629bbd9166fd4a Mon Sep 17 00:00:00 2001 From: John Lindgren Date: Fri, 8 Mar 2024 03:18:44 -0500 Subject: [PATCH] Apply new platform palette without overriding app palette Currently when a palette is set by qt6ct, it can in some cases override an app-specific palette. It would be nice if qt6ct would only set the platform palette (i.e. QPlatformTheme::SystemPalette) and still allow apps to set/adjust their own palette when desired. Test case: start Audacious and select Dark theme in Audacious settings. Then in qt6ct configuration, select "dusk" or "sand" color scheme. It partially applies to Audacious (where it should not) and you end up with an unusable mix of Audacious's color scheme and qt6ct's. Port of the qt5ct patch from https://sourceforge.net/p/qt5ct/tickets/97/. I have been using the patch in both qt5ct and qt6ct for quite some time and have not seen any ill side effects. https://github.com/trialuser02/qt6ct/pull/48 --- src/qt6ct-qtplugin/qt6ctplatformtheme.cpp | 34 ++++++----------------- src/qt6ct-qtplugin/qt6ctplatformtheme.h | 3 +- 2 files changed, 9 insertions(+), 28 deletions(-) diff --git a/src/qt6ct-qtplugin/qt6ctplatformtheme.cpp b/src/qt6ct-qtplugin/qt6ctplatformtheme.cpp index dca9423..461bef7 100644 --- a/src/qt6ct-qtplugin/qt6ctplatformtheme.cpp +++ b/src/qt6ct-qtplugin/qt6ctplatformtheme.cpp @@ -92,8 +92,9 @@ QPlatformDialogHelper *Qt6CTPlatformTheme::createPlatformDialogHelper(DialogType const QPalette *Qt6CTPlatformTheme::palette(QPlatformTheme::Palette type) const { - qDebug() << Q_FUNC_INFO << type; - return (m_usePalette && m_palette) ? m_palette.get() : QGenericUnixTheme::palette(type); + if (type == QPlatformTheme::SystemPalette && !m_isIgnored) + return &m_palette; + return QPlatformTheme::palette(type); } const QFont *Qt6CTPlatformTheme::font(QPlatformTheme::Font type) const @@ -152,21 +153,10 @@ void Qt6CTPlatformTheme::applySettings() { if(!QGuiApplication::desktopSettingsAware() || m_isIgnored) { - m_usePalette = false; m_update = true; return; } - if(!m_update) - { - //do not override application palette - if(QCoreApplication::testAttribute(Qt::AA_SetPalette)) - { - m_usePalette = false; - qCDebug(lqt6ct) << "palette support is disabled"; - } - } - QGuiApplication::setFont(m_generalFont); //apply font #ifdef QT_WIDGETS_LIB @@ -182,12 +172,6 @@ void Qt6CTPlatformTheme::applySettings() Qt6CT::reloadStyleInstanceSettings(); } - if(!m_palette) - m_palette = std::make_unique(qApp->style()->standardPalette()); - - if(m_update && m_usePalette) - qApp->setPalette(*m_palette); - if(m_userStyleSheet != m_prevStyleSheet) { // prepend our stylesheet to that of the application @@ -209,7 +193,10 @@ void Qt6CTPlatformTheme::applySettings() #endif if(m_update) + { QIconLoader::instance()->updateSystemTheme(); //apply icons + QGuiApplication::setPalette(QGuiApplication::palette()); //apply palette + } #ifdef QT_WIDGETS_LIB if(hasWidgets() && m_update) @@ -218,8 +205,6 @@ void Qt6CTPlatformTheme::applySettings() { QEvent e(QEvent::ThemeChange); QApplication::sendEvent(w, &e); - if(m_palette && m_usePalette) - w->setPalette(*m_palette); } } #endif @@ -250,17 +235,16 @@ void Qt6CTPlatformTheme::updateSettings() void Qt6CTPlatformTheme::readSettings() { - m_palette.reset(); - QSettings settings(Qt6CT::configFile(), QSettings::IniFormat); settings.beginGroup("Appearance"); m_style = settings.value("style", "Fusion").toString(); + m_palette = *QPlatformTheme::palette(SystemPalette); QString schemePath = settings.value("color_scheme_path").toString(); if(!schemePath.isEmpty() && settings.value("custom_palette", false).toBool()) { schemePath = Qt6CT::resolvePath(schemePath); //replace environment variables - m_palette = std::make_unique(Qt6CT::loadColorScheme(schemePath, *QPlatformTheme::palette(SystemPalette))); + m_palette = Qt6CT::loadColorScheme(schemePath, m_palette); } m_iconTheme = settings.value("icon_theme").toString(); //load dialogs @@ -338,8 +322,6 @@ void Qt6CTPlatformTheme::readSettings() QCoreApplication::setAttribute(Qt::AA_ForceRasterWidgets, true); else if(!m_isIgnored && forceRasterWidgets == Qt::Unchecked) QCoreApplication::setAttribute(Qt::AA_ForceRasterWidgets, false); - if(m_isIgnored) - m_usePalette = false; settings.endGroup(); } } diff --git a/src/qt6ct-qtplugin/qt6ctplatformtheme.h b/src/qt6ct-qtplugin/qt6ctplatformtheme.h index fd348d0..0a1d1af 100644 --- a/src/qt6ct-qtplugin/qt6ctplatformtheme.h +++ b/src/qt6ct-qtplugin/qt6ctplatformtheme.h @@ -80,7 +80,7 @@ private slots: #endif QString loadStyleSheets(const QStringList &paths); QString m_style, m_iconTheme, m_userStyleSheet, m_prevStyleSheet; - std::unique_ptr m_palette; + QPalette m_palette; QFont m_generalFont, m_fixedFont; int m_doubleClickInterval; int m_cursorFlashTime; @@ -88,7 +88,6 @@ private slots: int m_buttonBoxLayout; int m_keyboardScheme; bool m_update = false; - bool m_usePalette = true; int m_toolButtonStyle = Qt::ToolButtonFollowStyle; int m_wheelScrollLines = 3; bool m_showShortcutsInContextMenus = false;