Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt- Add custom font from resource

Tags:

I added this font to resource: BYekan.ttf
I want to use this font in my application. I've tried this :

    QFont font(":/images/font/BYekan.ttf");     nLabel->setFont(font);     nLabel->setText(tr("This is for test"));     layout->addWidget(nLabel); 

But, I guess it's not working. How to use it?

Edit: After reading this question , I've tried again :

int fontID(-1); bool fontWarningShown(false); QFile res(":/images/font/Yekan.ttf"); if (res.open(QIODevice::ReadOnly) == false) {     if (fontWarningShown == false) {         QMessageBox::warning(0, "Application", (QString)"Impossible d'ouvrir la police " + QChar(0x00AB) + " DejaVu Serif " + QChar(0x00BB) + ".");         fontWarningShown = true;     } }else {     fontID = QFontDatabase::addApplicationFontFromData(res.readAll());     if (fontID == -1 && fontWarningShown == false) {         QMessageBox::warning(0, "Application", (QString)"Impossible d'ouvrir la police " + QChar(0x00AB) + " DejaVu Serif " + QChar(0x00BB) + ".");         fontWarningShown = true;      }     else         nLabel->setFont(QFont(":/images/font/Yekan.ttf", 10)); } 

I compare this font and other font, but there isn't any different on Qt. why?

like image 472
Farzan Najipour Avatar asked Jun 22 '15 07:06

Farzan Najipour


People also ask

Where are QT fonts stored?

Qt normally uses fontconfig to provide access to system fonts. If fontconfig is not available, e.g. in dedicated embedded systems where space is at a premium, Qt will fall back to using QBasicFontDatabase . In this case, Qt applications will look for fonts in Qt's lib/fonts/ directory.

What is the default Qt font?

Qt uses 9px as default font size, but users (on Linux) can change this by using qtconfig .


1 Answers

int id = QFontDatabase::addApplicationFont(":/fonts/monospace.ttf"); QString family = QFontDatabase::applicationFontFamilies(id).at(0); QFont monospace(family); 
like image 53
Ankur Avatar answered Sep 21 '22 15:09

Ankur