Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change default application font

Tags:

qt

qtwidgets

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?

like image 884
Romário Avatar asked Feb 04 '17 14:02

Romário


People also ask

How do I set default font in apps?

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.

How do I change Windows default font?

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.

How do I change the font on my apps in Windows 10?

Open Settings. Click on Personalization. Click on Fonts. Select the font family you want to use.

How do I remove Calibri as my default font?

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.


2 Answers

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.

like image 50
kefir500 Avatar answered Sep 18 '22 04:09

kefir500


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.

like image 29
Spencer Avatar answered Sep 19 '22 04:09

Spencer