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?
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.