Seriousl Lancerl
Seriousl Lancerl

Reputation: 135

How to load multiple font of same familiy

I am writing an application using the Qt framework. In the display, I have to show multiple information, but using different types of font of the same family, Montserrat.

What I have done so far to load the fonts is:

int ultralightid = QFontDatabase::addApplicationFont(":/Montserrat_UltraLight.tff");
QString UltraFont= QFontDatabase::applicationFontFamilies(ultralightid ).at(0);
QFont font1(UltraFont,QFont::Normal);
font1.setPixelSize(50);

int lightid = QFontDatabase::addApplicationFont(":/Montserrat_Light.tff");
QString LightFont= QFontDatabase::applicationFontFamilies(lightid).at(0);
QFont font2(LightFont,QFont::Normal);
font2.setPixelSize(150);


label1->setFont(font1);
label2->setFont(font2);

label1->setText("bla bla");
label2->setText("bla bla");

The font sizes are correct, but the font itself it is not. From what I have noticed (trying with Hairline_Montserrat,Light_Montserrat,UltraLight_Montserrat), it is as if the fonts have a sort of priority. If I declare them all, all the fonts are the Light one, if I comment that font type, all of them are Hairline one, otherwise (last priority) the labels use the ultralight font.

I have tried adding other font type (from other families) and in that case my code works correctly.

If I use

 qDebug()<<QFontDatabase::applicationFontFamilies(ultralightid);
 qDebug()<<QFontDatabase::applicationFontFamilies(lightid);

both of them print the family "Montserrat". I use the qrc file and the AUTORCC flag in the CMAKE (it should be similar using qmake) and all the file are uploaded correctly.

Do you know if there is another way to add fonts of the same family? Or is there something I am doing wrong?

Here are the fonts:

Upvotes: 5

Views: 1678

Answers (2)

iperov
iperov

Reputation: 514

You don't need to manage font files from one family separatelly.

I suggest this solution:

  1. Create a folder with all ttf's of the same family.

  2. Load all files from the folder via id = QFontDatabase.addApplicationFont(path)

  3. Collect all font families from these files via QFontDatabase.applicationFontFamilies(id)

  4. Check if only one and desired family is loaded, and the family name is exactly the same as requested, or warn the user about these errors.

  5. Create font object font = QFont(family)

  6. Then for example, font.setItalic(True). If Italic version of family is loaded, it will be used, otherwise it will be created from Regular by QT.

Upvotes: 1

Slugger
Slugger

Reputation: 675

This is an old question but I was just struggling with exactly the same problem when trying to load normal, bold, ... versions of a font family in Qt.

I solved the problem (although in a somewhat hacky way) by simply giving each of the ttf files a different family name. I used Typograf, simply open the font, right click to open properties and then click rename. There are probably many other tools that do this too.

Upvotes: 4

Related Questions