简体   繁体   English

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

[英]Qt- Add custom font from resource

I added this font to resource: BYekan.ttf我将此字体添加到资源: 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.我比较了这种字体和其他字体,但在 Qt 上没有任何不同。 why?为什么?

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

在 QML 中,您可以

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

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.原因是,在调用函数 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.现在它对我有用(在 Linux 上使用 Qt 5.5.1 和 Qt 4.8.6 进行测试)并返回 0。在 Windows 上,可能需要在前面添加一个驱动器号。

Note: I had to provide the full path to the font file (eg /usr/share/fonts/ttf/droid/DroidSansFallbackFull.ttf)注意:我必须提供字体文件的完整路径(例如 /usr/share/fonts/ttf/droid/DroidSansFallbackFull.ttf)

No, see, I didn't do any of this.不,看,我没有做任何这些。 So for me, what I did was there's a ~/.font/ directory, if it doesn't exist you can create it.所以对我来说,我所做的是有一个~/.font/目录,如果它不存在你可以创建它。

Now you copy the ttf into this directory, and Linux will see it.现在你把 ttf 复制到这个目录下,Linux 会看到它。 However, in my case, I'm writing a QT application, so these fonts have names, so how does Linux know the name?但是,就我而言,我正在编写一个 QT 应用程序,因此这些字体都有名称,那么 Linux 是如何知道名称的呢?

If you run the command:如果运行命令:

fc-list

It dumps out all the font information systemwide and you can search for the font that you've added.它会转储系统范围内的所有字体信息,您可以搜索已添加的字体。

The output looks something like this:输出如下所示:

...
/usr/share/texmf/fonts/opentype/public/lm/lmsans17-oblique.otf: Latin Modern Sans,LM Sans 17:style=17 Oblique,Italic
/home/XXX/.fonts/PAPYRUS.TTF: Papyrus:style=Regular,Normal,obyčejné,Standard,Κανονικά,Normaali,Normál,Normale,Standaard,Normalny,Обычный,Normálne,Navadno,Arrunta
/usr/share/fonts/X11/Type1/n019064l.pfb: Nimbus Sans L:style=Bold Condensed Italic
...

The parts of the output after the first colon on each line is the name of the font as it's seen from inside of Linux for that user.每行第一个冒号之后的输出部分是该用户从 Linux 内部看到的字体名称。 So these are "Latin Modern Sans,LM Sans 17" / "Papyrus" / "Nimbus Sans L".所以这些是“Latin Modern Sans,LM Sans 17”/“Papyrus”/“Nimbus Sans L”。 So Linux sees it, and all your applications running as your user will see them also, (Gimp, your Window Manager, QT applications etc etc etc)因此 Linux 会看到它,并且您以用户身份运行的所有应用程序也会看到它们,(Gimp、您的窗口管理器、QT 应用程序等)

Inside your QT application you call the one you are interested in, in my case i'm interested in Papyrus font:在您的 QT 应用程序中,您称其为您感兴趣的应用程序,就我而言,我对 Papyrus 字体感兴趣:

tabWidget->setFont( QFont( "Papyrus",10 ) );

And then, sure enough the QT application just picks up the font...然后,果然QT应用程序只是拿起字体......

If you wanted to make the font systemwide, then you'd have to locate the position of the font directories, from what i can see it's /usr/share/fonts/truetype/ you will need to create a subdirectory in there for your fonts but perhaps some other distros may be in a different location, you might want to double check that.如果你想让字体在系统范围内,那么你必须找到字体目录的位置,从我可以看到它是/usr/share/fonts/truetype/你需要在那里为你的字体创建一个子目录但也许其他一些发行版可能位于不同的位置,您可能需要仔细检查一下。 Anyhow you can dump the ttf files in there.无论如何,您可以将 ttf 文件转储到那里。 If you do that, you might want to consider running fc-cache -fv as this will treewalk through the truetype subdirectories seeking out newly added fonts.如果这样做,您可能需要考虑运行fc-cache -fv因为这将遍历 truetype 子目录以查找新添加的字体。

With anything font related under Linux, run fc-list .对于 Linux 下的任何字体,运行fc-list It clears up all sorts of confusions and missunderstandings and sheds light on the otherwise dark and mysterious world of Linux fonts.它清除了各种混乱和误解,并揭示了 Linux 字体原本黑暗而神秘的世界。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM