How to embed my own font in my application

I want to add a custom font to my application, and I already added it to my resource file.

And my code is as follows:

int id = QFontDatabase::addApplicationFont(":/fonts/ae_AlMateen.ttf"); QMessageBox::information(this,"Message",QString::number(id)); 

Also the contents of the .qrc file.

 <RCC> <qresource prefix="/fonts"> <file alias="ae_AlMateen">ae_AlMateen.ttf</file> </qresource> </RCC> 

But the problem is that addApplicationFont always returns -1 .

Please note that when changing :/fonts/ae_AlMateen.ttf to the direct path ex: C://ae_AlMateen.ttf it works fine.

I want the font file to be integrated with the application executable so that the application does not need to attach a font file to it.

+6
source share
2 answers

Ahhh ... now, after you have added your .qrc, I understand. Easy to explain:

 <file alias="ae_AlMateen">ae_AlMateen.ttf</file> 

You have added an alias to your .qrc file. If you remove alias = "ae_AlMateen" , it will work as expected ... with the extension .ttf.

0
source

If you also use qml, you can load the font in the qml file this way . I recommend it.

And if you still want to download the font from the cpp file, read this article , this may help you.

Edit : The following code may work on Qt5.4.1 on OSX10.10. (The font is embedded in the executable file)

 int id = QFontDatabase::addApplicationFont(":/fonts/fontawesome-webfont.ttf"); QMessageBox::information(NULL,"Message",QString::number(id)); // this shows id is 0. QFont font; font.setFamily("FontAwesome"); font.setPointSize(30); ui->commandLinkButton->setFont(font); ui->commandLinkButton->setText("\uf021"); // this shows the Refresh icon. 

Edit2 : I did another test on Win7 with Qt5.4.1 (msvc2013 64bit). The font is embedded in the exe file. Everything is working fine.

+1
source

Source: https://habr.com/ru/post/986785/


All Articles