Qt 中获取字体的像素高度和宽度

 

最近在用Qt做文字的滚屏效果,要计算字体的像素高度和像素宽度,用到以下几个函数:

 

1. QLabel::fontMetrics().width(QString s): 获取字符串s的总像素宽度。

 int QFontMetrics::width ( const QString & text, int len = -1 ) const

Returns the width in pixels of the first len characters of text. If len is negative (the default), the entire string is used.

Note that this value is not equal to boundingRect().width(); boundingRect() returns a rectangle describing the pixels this string will cover whereas width() returns the distance to where the next string should be drawn.

See also boundingRect().

  

2. QLabel::fontMetrics().height(): 获取字体的高度。

 

int QFontMetrics::height () const

Returns the height of the font.

This is always equal to ascent()+descent()+1 (the 1 is for the base line).

See also leading() and lineSpacing().

 3. QLabel::fontMetrics().lineSpacing(): 获取字体的高度,包括文字的实际宽度和行距。

  4. QLabel::fontMetrics().leading(): 行间距

 int QFontMetrics::leading () const

Returns the leading of the font.

This is the natural inter-line spacing.

See also height() and lineSpacing().

 

 其他函数参见QT 的 QFontMetrics Class Reference 

 

int QFontMetrics::lineSpacing () const

Returns the distance from one base line to the next.

This value is always equal to leading()+height().

See also height() and leading().

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值