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

你可能感兴趣的:(String,Class,qt,distance)