I started to learn and use PySide2 and Qt about 2 weeks ago. I am working on a personal project based on PySide2 and I am trying to add a change font feature.
I found that the frame of QPushButton
looks different while I am using some fonts.
No matter it is dark mode or light mode, the buttons look different. When I using fonts like DejaVu Sans Mono for Powerline, it is the macOS default looking, but when I change it to fonts like JetBrains Mono, it becomes another style. Other controls do not have this problem. Both fonts are based on ttf files and I do not figure out the relationship between the looking and the font.
What could be the reason and how can I keep the macOS default looking no matter what font I use?
Some information about the system and environments:
- macOS Big Sur 11.3.1
- Python 3.9.4
- PySide2==5.15.2
And here is the source could reproduce this problem.
import os
import sys
os.environ["QT_MAC_WANTS_LAYER"] = "1" # https://stackoverflow.com/a/66204842/1166461
from PySide2 import QtGui
from PySide2 import QtWidgets
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QWidget()
layout = QtWidgets.QVBoxLayout()
fonts = QtWidgets.QComboBox(window)
fonts.insertItems(0, QtGui.QFontDatabase().families(QtGui.QFontDatabase.Latin))
fonts.currentIndexChanged.connect(lambda i: QtWidgets.QApplication.instance().setStyleSheet(f'''
* {{
font-family: "{fonts.itemText(i)}";
}}
'''))
layout.addWidget(fonts)
layout.addWidget(QtWidgets.QPushButton('I am a push button', parent=window))
window.setLayout(layout)
window.show()
sys.exit(app.exec_())