QFontDatabase不包含所有字体家族或它有不同的名称

QFontDatabase不包含所有字体家族或它有不同的名称

本文介绍了QFontDatabase不包含所有字体家族或它有不同的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个QWidget的默认字体的家庭使用font()。family()。我比较这与QStringList我从QFontDatabase()。families()。默认字体的家庭是Sans,但我不能在从QFontDatabase获得的列表中,我只能找到Sans Serif,Droid Sans,FreeSans等。如何QWidget的默认字体是甚至不存在的系统的字体?

解决方案

这是一个经典之旅。



QFont 是字体的请求。它可能由不匹配所请求的东西满足。 实际字体可从 QFontInfo 获得。



可以在 QFont 中放置任何。在什么时候应该 QFont 更改本身以指示实际选择的字体?如果你在一个小部件上设置一个字体,然后读回它,它被改变,以匹配什么字体在那里是相当困惑。所以,没有这样的合理点,其中 QFont 可以变形,因此 QFont 不能只是一个请求。 / p>

您控制 QFont ,但系统的字体可用性和其他约束控制匹配 QFontInfo



不变量可以表示为:

  QFontDatabase db; 
QFont const font = widget-> font();
QStringList const families = db.families();
Q_ASSERT(families.contains(font.family())|| true);
QFontInfo const info(font);
Q_ASSERT(families.contains(info.family()));


I'm getting a QWidget's default font's family using font().family(). I compare this against the QStringList I get from QFontDatabase().families(). The default font's family is "Sans" but I cannot find that in the list I get from QFontDatabase, I can only find Sans Serif, Droid Sans, FreeSans etc. How come QWidget's default font is something that is not even present on the system's fonts?

解决方案

This is a classic trip-up.

Logically, QFont is a request for a font. It may be satisfied by something that doesn't quite match what was requested. The actual font is available from QFontInfo.

If you think about it, you can put "whatever" in a QFont. At what point should QFont change itself to indicate what font was actually selected? It'd be rather baffling if you set a font on a widget, then read it back, and it got changed to match what fonts are there. So, there's no such reasonable point where a QFont could morph, so QFont can't be but a request.

You control QFont, but the system's font availability and other constraints controls the matching QFontInfo.

The invariant may be expressed as:

QFontDatabase db;
QFont const font = widget->font();
QStringList const families = db.families();
Q_ASSERT(families.contains(font.family()) || true);
QFontInfo const info(font);
Q_ASSERT(families.contains(info.family()));

这篇关于QFontDatabase不包含所有字体家族或它有不同的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 11:13