The QFontMetrics class provides font metrics information.
QFontMetrics functions calculate the size of characters and strings for a given font. There are three ways you can create a QFontMetricsobject:
- Calling the QFontMetrics constructor with a QFont creates a font metrics object for a screen-compatible font, i.e. the font cannot be a printer font. If the font is changed later, the font metrics object is not updated.
(Note: If you use a printer font the values returned may be inaccurate. Printer fonts are not always accessible so the nearest screen font is used if a printer font is supplied.)
- QWidget::fontMetrics() returns the font metrics for a widget's font. This is equivalent to QFontMetrics(widget->font()). If the widget's font is changed later, the font metrics object is not updated.
- QPainter::fontMetrics() returns the font metrics for a painter's current font. If the painter's font is changed later, the font metrics object is not updated.
Once created, the object provides functions to access the individual metrics of the font, its characters, and for strings rendered in the font.
QTableWidget* table = ...;
QFontMetrics fm(table->font());
int pixelsWidth = fm.width("65535");
int pixelsHeight = fm.height();QFont font("times", 24);
QFontMetrics fm(font);
int pixelsWide = fm.width("What's the width of this text?");
int pixelsHigh = fm.height();
本文介绍 QFontMetrics 类的功能及用法,该类提供了字体度量信息,包括字符和字符串尺寸的计算方法。文章详细解释了三种创建 QFontMetrics 对象的方式,并通过实例展示了如何获取字体的高度和宽度。
563

被折叠的 条评论
为什么被折叠?



