Comments (1)
Qt 里正好有一个类是做这件事的: QFontMetrics .
它可以计算所给字体的字符甚至字符串的 size .创建一个QFontMetrics 对象有三种方式:
- 构造函数, QFontMetrics(const QFont &font) ,但要注意的是, 这个字体不能是 painter font (可能会不准确). 如果该字体发生变化, 对象不会自动更新.
- QWidget::fontMetrics() == QFontMetrics(widget->font()). 同样的, 如果 widget 的字体变化, 不会随之更新.
- QPainter::fontMetrics() 返回 painter 当前的字体, 还是, 如果 painter 的字体变化, 不会随之更新.
拿到对象,就可以开搞了,看看有些什么工具:
- ascent() : 基线到顶部的距离,实际运用时可能会非常小(非英文字体,个性字体)。
- descent() : 基线到底部的距离,和上面一样,可能非常小。
- height() : 字体高度, = ascent() + descent() + 1,1是baseline的高度。
- leading() : 行距。(This is the natural inter-line spacing.)
- lineSpacing() : 相邻 baseline 的距离。
再看些高级的:
- underlinePos() : 基线到下划线(画出来的话)的距离。
- overlinePos() : 基线到上划线(画出来的话)的距离。
- strikeOutPos() : 基线到删除线(画出来的话)的距离。
- lineWidth() : 下划线和删除线的宽度。
These functions are all fast.
- minLeftBearing() : 最小左间距
- minRightBearing() : 最小右间距
- maxWidth() : 最大字宽
_These are by necessity slow, and we recommend avoiding them if possible._
具体用法:
对于每一个 character, 可以用 width(), leftBearing(), rightBearing(), inFont()(用来确认该字是否在字体库内), 也可以把 character 当成 string.
对于每一个 string, 可以用 width(), boundingRect(), size(). 一个返回 QRect, 一个返回 QSize.
QFont font("times", 24);
QFontMetrics fm(font);
int pixelsWide = fm.width("What's the width of this text?");
int pixelsHigh = fm.height();
参考资料:
- Qt Reference Documentation
- III. Glyph Metrics
这个话题可以更加深入研究:
from qtlab.
Related Issues (12)
- 如何制作一个自适应缩放的坐标轴 HOT 18
- 如何在 Xcode 中进行 Qt 开发? HOT 1
- 如何获取鼠标滚轮滚过的距离? HOT 1
- QWidget 那么多获取边框的方法有啥区别?该怎么用? HOT 1
- 如何在QPainter里用鼠标滑动一根线。 HOT 2
- 如何变换鼠标的指针形状?都有哪些指针形状? HOT 1
- 能不能对QPainter中的线条增加右键菜单?并通过菜单修改颜色或删除? HOT 1
- 通过 QLinearGradient 进行线性插值之后,如何获取中间某位置的特定颜色? HOT 14
- 如何设置界面风格? HOT 2
- vs2010下无法创建Translation File HOT 1
- 想学习下Qt,讨论一下关注点. HOT 16
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from qtlab.