From c68bc3a09add939b58f2513a39485cb3bf23b404 Mon Sep 17 00:00:00 2001 From: Maksim Totmin Date: Sun, 31 May 2026 15:01:17 +0700 Subject: [PATCH] Add GTK theme integration for Linux desktop environments - Add _setup_linux_theming() called before QApplication creation - Auto-detect libqgtk3.so plugin from common Qt6 plugin paths - Sets QT_QPA_PLATFORMTHEME=gtk3 so Qt follows system GTK theme - Falls back to qt6ct if gtk3 plugin not found - Falls back to Fusion style if no platform theme available - Respects user-set QT_QPA_PLATFORMTHEME (never overrides) --- src/main.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/main.py b/src/main.py index 4685f5f..7150f73 100644 --- a/src/main.py +++ b/src/main.py @@ -1043,8 +1043,53 @@ class MainWindow(QMainWindow): event.accept() +# GTK3 platform theme paths for Linux desktop integration +_GTK3_PLUGIN_PATHS = [ + "/usr/lib/qt6/plugins/platformthemes/libqgtk3.so", + "/usr/lib/x86_64-linux-gnu/qt6/plugins/platformthemes/libqgtk3.so", + "/usr/lib64/qt6/plugins/platformthemes/libqgtk3.so", + "/usr/lib/aarch64-linux-gnu/qt6/plugins/platformthemes/libqgtk3.so", +] + + +def _setup_linux_theming(): + """Configure Qt to follow the system GTK theme on Linux. + + Must be called BEFORE QApplication is created. + Respects user-set QT_QPA_PLATFORMTHEME (doesn't override it). + Falls back through available platform themes: gtk3 → qt6ct → none. + """ + if sys.platform != "linux": + return + + if "QT_QPA_PLATFORMTHEME" in os.environ: + return # User already set it + + # Check for libqgtk3.so (official Qt GTK integration) + for path in _GTK3_PLUGIN_PATHS: + if os.path.exists(path): + os.environ["QT_QPA_PLATFORMTHEME"] = "gtk3" + logger.debug(f"Using GTK3 platform theme: {path}") + return + + # Check for qt6ct (Qt configuration tool) + if os.path.exists("/usr/bin/qt6ct"): + os.environ["QT_QPA_PLATFORMTHEME"] = "qt6ct" + logger.debug("Using qt6ct platform theme") + return + + logger.debug("No GTK platform theme plugin found, using Qt default style") + + if __name__ == "__main__": + _setup_linux_theming() + app = QApplication(sys.argv) + + # Apply Fusion as fallback style if no platform theme was applied + if not os.environ.get("QT_QPA_PLATFORMTHEME"): + app.setStyle("Fusion") + window = MainWindow() window.show() sys.exit(app.exec())