mardi 21 juillet 2020

Qt: 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 type 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.

i 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 the fonts:

[https://www.onlinewebfonts.com/download/9d31c906a6cc6064bbe7d33d51058317][1] light [https://it.allfont.net/download/montserrat-light/][2] ultralight

Thanks

Aucun commentaire:

Enregistrer un commentaire