如何在Qt中使用QFont获取字体文件路径?

Jul*_*vit 7 fonts filenames qt filepath

我想检索其扩展名的字体文件路径(例如"arial.ttf").

QFont :: rawname()方法总是返回" 未知 ".

有没有其他方法来获取字体的名称和扩展名?

这是我们使用的代码:

    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
    }
Run Code Online (Sandbox Code Playgroud)

Rei*_*ica 5

QFont 是对字体的请求,而不是对匹配的实际字体的描述;后者是QFontInfo。请参阅我的另一个答案。不幸的是,QFontInfo不给你rawName()。有一种迂回的方式来获取它,如果有的话,你不能保证它可以在所有平台上工作。

QFont myFont;
QFontInfo info(myFont);
QFont realFont(info.family());
QString rawName = realFont.rawName(); 
Run Code Online (Sandbox Code Playgroud)

如果您正在寻找字体等内容的标准位置,那么在 Qt 5 中您可以QStandardPaths::standardLocations使用FontsLocation. 在 Qt 4 中,您可以使用QDesktopServices::storageLocation.