牛骨文教育服务平台(让学习变的简单)
博文笔记

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

创建时间:2011-04-06 投稿人: 浏览次数:180

最近在用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().

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。