Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the font file path with QFont in Qt?

I would like to retrieve the font file path with its extension (for example "arial.ttf").

The QFont::rawname() method always returns "unknown".

Is there another way to obtain the name and the extension of a font?

Here is the code we use:

    bool ok = true;
    QFont font = QFontDialog::getFont(&ok, this);
    if(ok)
    {
    QString fontpath = "/Library/Fonts/" + font.family()+".ttf";//Using font file path in the application
    }
like image 310
Julien Ravit Avatar asked Sep 30 '13 15:09

Julien Ravit


1 Answers

A QFont is a request for a font, not a description of an actual font that matched; the latter is QFontInfo. See my other answer. Unfortunately, QFontInfo does not give you rawName(). There is a roundabout way to get it if at all, you're not guaranteed that it will work on all platforms.

QFont myFont;
QFontInfo info(myFont);
QFont realFont(info.family());
QString rawName = realFont.rawName(); 

If you're looking for standard locations of things such as fonts, then in Qt 5 you'd use QStandardPaths::standardLocations with FontsLocation. In Qt 4, you'd use QDesktopServices::storageLocation.

like image 190
Kuba hasn't forgotten Monica Avatar answered Sep 20 '22 03:09

Kuba hasn't forgotten Monica