QFontMetrics|Qt

本文介绍 QFontMetrics 类的功能及用法,该类提供了字体度量信息,包括字符和字符串尺寸的计算方法。文章详细解释了三种创建 QFontMetrics 对象的方式,并通过实例展示了如何获取字体的高度和宽度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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:

  1. 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.)

  2. 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.
  3. 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();


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值