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內部計算字體、像素和字符寬度之間的方法是:

見地址:http://losemyheaven.blog.163.com/blog/static/17071980920111283934557/

—————————————ZZ -start ————————————

在QFont當中有兩種方式設置字體大小,一種是PixelSize,另一種是PointSize

Point實際是磅,也就是 1/72 inch

我們可以從PainterDevice中得到當前DPI(Dot per inch)。因此 Pixel = DPI * Point / 72

例如設置字體爲20Point。 那麼字體的像素大小是 90 * 20 / 72 約等於 25.


可以將字體設置爲25Pixel,看到字體的大小就是20Point和25Pixel大小是一樣的。

QFont當中設置的大小,實際上字體的高度,由如下組成。

由PixelSize設置的高度,實際是Asent + 1像素(baseline的高度)。

在字體大小中涉及了以下知識:

DPI: Dot per inch,在顯示器上也就是每英寸包含的像素。英尺的換算爲 1 cm = 0.39 inch, 1 inch = 2.54 cm。

DPI 可以通過如下計算得來:

水平 dpi = 水平 resolution * 2.54 / 顯示器寬度

垂直 dpi = 垂直 resolution * 2.54 / 顯示器高度

pixel pitch(點距): 像素點間距離。可以用25.4mm / DPI得到。

字體在屏幕上的實際大小 = 字體像素大小 * 點距

大部分顯示器的DPI爲90.6左右,垂直和水平點距可能不一樣。但windows爲了方便就同一了點距,爲96。我當前使用的Ubuntu的點距也是96。因此如果想要看到更大的字體可以使用更大的點距。

發佈了59 篇原創文章 · 獲贊 27 · 訪問量 36萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章