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?
Open any Word document. Right-click somewhere in the document and choose “Font”. In the Font dialog box, select your preferred typeface and any other settings you want to change (e.g., font size). Click the “Set As Default” button.
Open the “Start” menu, search for “Settings,” then click the first result. You can also press Windows+i to quickly open the Settings window. In Settings, click “Personalization,” then select “Fonts” in the left sidebar. On the right pane, find the font that you want to set as the default and click the font name.
Open Settings. Click on Personalization. Click on Fonts. Select the font family you want to use.
Click the Default button at the bottom of the dialog box. A dialog box pops up asking if you want to make the changes to your Normal template. Click Yes. It's that simple.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With