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)
This commit is contained in:
Maksim Totmin 2026-05-31 15:01:17 +07:00
parent 56a88cf420
commit c68bc3a09a

View File

@ -1043,8 +1043,53 @@ class MainWindow(QMainWindow):
event.accept() 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__": if __name__ == "__main__":
_setup_linux_theming()
app = QApplication(sys.argv) 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 = MainWindow()
window.show() window.show()
sys.exit(app.exec()) sys.exit(app.exec())