How to instruct PySide2 application to use qt5ct settings?

Viewed 1603

I'm experimenting with very simple PySide2 tutorial examples and noticed that the style and font size are very different from the style of "normal" Qt applications.

I'm using qt5ct in Linux Mint 18 but PySide2 app ignores the settings:

enter image description here

The application is very simple:

import sys
from PySide2.QtUiTools import QUiLoader
from PySide2.QtWidgets import QApplication
from PySide2.QtCore import QFile, Qt

if __name__ == "__main__":
    app = QApplication(sys.argv)
    file = QFile("mainwindow.ui")
    file.open(QFile.ReadOnly)
    loader = QUiLoader()
    window = loader.load(file)
    window.show()
    sys.exit(app.exec_())

Is there any way of instructing it to use qt5ct settings? Google gives nothing useful so any hints are appreciated!

2 Answers

Found myself here a few times and eventually found my own solution.

See https://bugreports.qt.io/browse/PYSIDE-842

This issue seems to occur when your pyside2 was installed via pip but your Qt was installed by the system (Manjaro/Arch in my case).

This is what I got when using the pip version of pyside2:

>>> from PySide2.QtWidgets import QStyleFactory
>>> print(QStyleFactory.keys())

[u'Windows', u'Fusion']

When using the pyside2 packaged by arch linux I get:

>>> from PySide2.QtWidgets import QStyleFactory
>>> print(QStyleFactory.keys())

['Adwaita-Dark', 'Adwaita', 'Breeze', 'bb10dark', 'bb10bright', 'kvantum-dark', 'kvantum', 'cleanlooks', 'gtk2', 'cde', 'motif', 'plastique', 'qt5ct-style', 'Oxygen', 'Windows', 'Fusion']

Using the pyside2 package that came with my system instead (sudo pacman -S pyside2) solved this for me. My pyside2 app now uses the system Qt theme.

In order for all your applications to use qt5ct you must establish QT_QPA_PLATFORMTHEME = "qt5ct" as a variable of your session or system, for example in the ~/.xprofile:

[ "$XDG_CURRENT_DESKTOP" = "KDE" ] || [ "$XDG_CURRENT_DESKTOP" = "GNOME" ] || export QT_QPA_PLATFORMTHEME="qt5ct"

If you only want to be established in only your application, you must pass it through the command line:

python /path/of/your/script.py --platformtheme qt5ct

Or better add it by code:

import sys
from PySide2 import QtCore, QtWidgets, QtUiTools


if __name__ == "__main__":
    sys.argv.extend(["--platformtheme", "qt5ct"]) 
    app = QtWidgets.QApplication(sys.argv)
    file = QtCore.QFile("mainwindow.ui")
    file.open(QtCore.QFile.ReadOnly)
    loader = QtUiTools.QUiLoader()
    window = loader.load(file)
    window.show()
    sys.exit(app.exec_())
Related