Change default application font

The only way I have figured out to change the font for a whole application in Qt is with stylesheets. For PyQt in the application's init class you can call self.setStyleSheet('QWidget {font: "Roboto Mono"}'). Due to the cascading nature of stylesheets this will set the font of all widgets to Roboto Mono.

Just setting QApplication.setFont(font) has not always worked for me. Sometimes deeply nested child widgets do not seem to respect the font, such as headers in QTreeView.


Simply use the setFont() method on the QApplication or QWidget:

QFont font("Courier New");
font.setStyleHint(QFont::Monospace);
QApplication::setFont(font);

Note the setStyleHint(QFont::Monospace) line: it ensures that even if the specified font family is not present in the system, another suitable monospace font will be used.


Also, in my opinion it's better to set font for a certain widget, not the whole application: this gives you a more structured code for your UI in case of its expansion. However, this is still a matter of a design, of course.

Tags:

Qt

Qtwidgets