Change default application font

Romário picture Romário · Feb 4, 2017 · Viewed 17k times · Source

I have a Qt application that parses some JSON files and outputs its contents. I want the output to be in a monospaced font and the simplest way to do that is changing the default font of the entire application to a monospace. How do I do that in Qt?

Answer

kefir500 picture kefir500 · Feb 4, 2017

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.