C++ Qt-从资源添加自定义字体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30973781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:50:38  来源:igfitidea点击:

Qt- Add custom font from resource

c++qtfonts

提问by Farzan Najipour

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

我将此字体添加到资源:BYekan.ttf
我想在我的应用程序中使用此字体。我试过这个:

    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?

我比较了这种字体和其他字体,但在 Qt 上没有任何不同。为什么?

回答by Ankur

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

回答by kyb

In QML you can

在 QML 中,您可以

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

回答by Ernie Mur

I had the same problem as reported in the original question. The above presented solution (answer beginning with the line "int id = QFontDatabase::addApplicationFont....) however did not work, as can be also seen in the comments above. addApplicationFont returned -1.

我遇到了与原始问题中报告的相同的问题。上面提出的解决方案(以“int id = QFontDatabase::addApplicationFont....”行开头的答案)但是不起作用,正如上面的评论中所见。addApplicationFont 返回-1。

The reason is, that there is a leading ':' in the string for the call of the function addApplicationFont. I removed this. Now it works for me (testet with Qt 5.5.1 and Qt 4.8.6 on Linux) and returns 0. On Windows it might be necessary to add a drive letter in front.

原因是,在调用函数 addApplicationFont 的字符串中有一个前导“:”。我删除了这个。现在它对我有用(在 Linux 上使用 Qt 5.5.1 和 Qt 4.8.6 进行测试)并返回 0。在 Windows 上,可能需要在前面添加一个驱动器号。

Note: I had to provide the full path to the font file (e.g. /usr/share/fonts/ttf/droid/DroidSansFallbackFull.ttf)

注意:我必须提供字体文件的完整路径(例如 /usr/share/fonts/ttf/droid/DroidSansFallbackFull.ttf)