2015-06-22 13 views
10

Ich habe diese Schrift zur Ressource hinzugefügt: BYekan.ttf
Ich möchte diese Schriftart in meiner Anwendung verwenden. Ich habe es versucht:Qt- Benutzerdefinierte Schrift von Ressource hinzufügen

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

Aber ich denke, es funktioniert nicht. Wie man es benutzt?

Edit: Nach this question lesen, ich habe wieder versucht:

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)); 
} 

ich diese Schrift und andere Schrift vergleichen, aber es ist nicht anders auf Qt. Warum?

+0

Versuchen QFont Schrift (":/images/font/BYekan.ttf"); nLabel-> setFont (Schriftart); – Ankur

+0

Ich habe es versucht, aber es funktioniert immer noch nicht – AFN

Antwort

21
int id = QFontDatabase::addApplicationFont(":/fonts/monospace.ttf"); 
QString family = QFontDatabase::applicationFontFamilies(id).at(0); 
QFont monospace(family); 
+0

Wie man es benutzt? nLabel-> setFont (Monospace (Familie)); Fehler: keine Übereinstimmung für den Aufruf an '(QFont) (QString &)' – AFN

+1

Nein, so. nLabel-> setFont (Monospace); – Ankur

+0

ID ist -1, wenn die Schriftart nicht geladen werden kann – Paglian

1

In QML können Sie

FontLoader { id: font; source: "/fonts/font.otf" } 
Verwandte Themen