2

Both this question Add a custom font in Qt and this link mentioning the ability to add fonts through c++. Is this possible through python and the Qt Designer framework? I have attempted adding the ttf to resources and compiling the qrc file. Then in the stylesheet defining the font.

When launching the app nothing happens. Is there something else that needs to be done? The example discusses loading binary files.

Community
  • 1
  • 1
user-2147482637
  • 2,115
  • 8
  • 35
  • 56
  • 1
    See [How to use Non-Standard Custom Font with Stylesheets](http://stackoverflow.com/q/27955654/984421): the important difference is that you need to load the font using a resource path, as suggested in the answer by GPPK. – ekhumoro Feb 06 '15 at 19:28

2 Answers2

4

You need to also add it to the QFontDatabase:

First add the font file “yourfont” to the resource editor (which you have done) and then to the fontdatabase

QFontDatabase fontDatabase; 
fontDatabase.addApplicationFont(":/fonts/yourfont");

Now you can use yourfont with setFont for instance on a QLabel.

GPPK
  • 6,546
  • 4
  • 32
  • 57
  • 1
    Where is this line added? and what does that first line do? – user-2147482637 Feb 08 '15 at 08:53
  • This is old, but for future reference, I had to use: `fontDatabase = QtGui.QFontDatabase()` followed by `fontDatabase.addApplicationFont("./relative/path/fontfile.tff")` placed somewhere after the QApplication is created. – wyattg71 Nov 16 '22 at 23:04
2

Since there is no mention in the answers/comments about QtDesigner, neither here nor in the linked posts, this is what I found about QtDesigner standalone (5.6) - about a font that is not installed in the system:

I've used a free font called AutobusBold.ttf. First, path to this file needs to end up in your qrc file, mostly for the sake of Python - you can use Resource Browser/Edit Resources (the pencil) for this; you will end up with something like this in the .qrc file:

<file>myfont/AutobusBold.ttf</file>

If you open the .ttf file with fontview.exe on Windows, you'll see its name is Autobus Bold - with a space.

Then you need to close the .ui file in QtDesigner, then go to Settings/Additional Fonts... in QtDesigner (if an .ui file is open, then this option is greyed out); there click the + ("Add font file"), find the .ttf font file, and add it by selecting it and hitting Open. From now on, this font will appear whenever the Font dialog is opened in QtDesigner, mixed with the system fonts.

Then, reopen your .ui file, find the element you want to apply this font to and select it, in its styleSheet option click the three dots, and from the resulting dialog window, click Add Font - you'll get the Font dialog, with all system fonts - but also the custom font will be there; select it, and you'll get a .qss stanza like this: font: 75 8pt "Autobus Bold";

That first 75 is probably font-weight, you can also delete it, it will work the same; the 8pt is font-size, and "Autobus Bold" is font-family. Notice here that the font name in font-family is quoted, as it has a space in the name - without the quotes, everything fails!

Then, if you write an extern .qss file, you can rewrite the font: stanza as separate font-family (with quoted font name!) and font-size stanzas - and it should work the same.

Of course, if you want to transfer all this to Python, then you need to additionally addApplicationFont as described in the answer by @GPPK.

sdbbs
  • 4,270
  • 5
  • 32
  • 87