4

I am trying to load a font into my application. Logically, I want to load more than one font weight, but that's where the problem arises.

I have two font files, "PFD-Black.ttf" and "PFD-Regular.ttf". I want to use them both, so I load them both using this code:

fontid_regular = QFontDatabase.addApplicationFont("PFD-Regular.ttf")
fontid_black = QFontDatabase.addApplicationFont("PFD-Black.ttf")

I then get the family name, like so:

familyname = fontdb.applicationFontFamilies(fontid_black)[0]

Note that I have tried using fontid_regular as well but to no avail. Then I attempt to use the font for a QLabel:

lab = QLabel("My Font Window")
font = QFont(familyname)
lab.setFont(font)
lab.show()

That is the extent of my code, other than the boilerplate imports and QApplication.exec_() call.

This does not work (which may be obvious since I'm asking a question on SO about it), the label shows up in the default font.

This is when is doesn't work

The baffling part about my problem, is that when I comment out the line fontid_regular = QFontDatabase.addApplicationFont("PFD-Regular.ttf") the bold font shows.

This is when it kind of works

This is some level of workingness (for lack of a better word), but I am not able to use the regular weight font. It should also be noted that commenting out the line where I load "PFD-Black.ttf" does not fix the issue.

Note that I have seen this question, but no matter what mutations I perform on my code, I cannot make that solution work for me, so please don't mark this question as a duplicate of that one.

Community
  • 1
  • 1
KFox
  • 1,166
  • 3
  • 10
  • 35
  • What is the value of `applicationFontFamilies` for both fonts? I suspect they are the same, so when you load both fonts, the first one is found by the family name, and when you didn't load the first font, the second one shows up. It could explain why commenting `fontid_regular` out changes the behavior. Try to use `font.setBold(true)` to use the second font while loading both fonts. – Pavel Strakhov Jan 03 '14 at 00:03
  • Its a good idea, but while I have both loaded, the default Helvetica or whatever it is is used as the font for the QLabel, not the lighter weight custom font. – KFox Jan 03 '14 at 00:15
  • And I have tried various combinations of `setBold` and commenting out lines, none of which worked. – KFox Jan 03 '14 at 00:17
  • Is it happening on Mac? Which version of it? – László Papp Jan 03 '14 at 01:06
  • @PavelStrakhov is right here, I think based on the internal code `families.append(QCFString(CTFontCopyFamilyName(font)));`. If `font = QFont(familyname); font.setBold(true);` does not work, file a bugreport on the following tracker: https://bugreports.qt-project.org/ – László Papp Jan 03 '14 at 01:15
  • @ekhumoro: works for me. What do you mean by "borked"? – László Papp Jan 03 '14 at 01:29
  • @ekhumoro: what I wrote was, "file a bugreport on the following tracker: bugreports.qt-project.org". Why would you have a bug number for a bug that is about to be filed? :D – László Papp Jan 03 '14 at 01:31
  • @LaszloPapp. Damn! Sorry, I must be hallucinating - I somehow read "like this bugreport..." – ekhumoro Jan 03 '14 at 01:33
  • Yes it is on a mac, version 10.8.4 – KFox Jan 03 '14 at 01:40
  • @KFox. What does `QFontDatabase().styles(fontname)` give you for each font? And what are the actual names (not filenames) of the fonts? Have tried loading the fonts using [QFontDatabase.font](https://qt-project.org/doc/qt-4.8/qfontdatabase.html#font)? – ekhumoro Jan 03 '14 at 02:01
  • When both fonts are being loaded, `QFontDatabase().styles(fontname)`, returns `[u'Normal', u'Bold']`, as I would expect. The names of both fonts are the same, `'Playfair Display'`. I had not thought of `QFontDatabase.font()`, but trying it with several combinations of other solutions all failed. – KFox Jan 03 '14 at 04:01
  • 1
    @KFox. Have you tried the solution in the other question you linked to using the same "Avenir" font? If that works, then it would seem that the issue is finding the equivalent munged names for your "Playfair Display" font. – ekhumoro Jan 03 '14 at 04:25
  • The "Avenir" font cost an amount of money I was not willing to pay, so I tried my code with several different fonts from Google Fonts, which worked much better. Different fonts needed different configurations though (some loaded different weights as different families, others loaded them all as one), do you know any way around that? – KFox Jan 03 '14 at 04:43
  • No, sorry. So are you saying that the problem is just with this specific "Playfair Display" font? – ekhumoro Jan 03 '14 at 04:45
  • Yes, the issue was with the font I suppose, thankyou for all your help! – KFox Jan 03 '14 at 04:46
  • Hmm, that was also one option when I discussed this with one of the developers behind, but I thought that would be the last guess since this kind of thing is unforunate. Glad you figured out. :) – László Papp Jan 03 '14 at 05:53
  • Is this behavior worthy of a bug report? I would guess not since it was fixed by using a different font. – KFox Jan 03 '14 at 17:13
  • Bugreport against what? Qt cannot be blamed for you having bad fonts. :D – László Papp Jan 04 '14 at 01:15
  • Thats what I assumed but I just wanted to make sure. :) – KFox Jan 04 '14 at 02:12

1 Answers1

1

The problem has been solved in the comments above: it turned out that there was a problem with the font I was using.

KFox
  • 1,166
  • 3
  • 10
  • 35