qfontmetrics :: MorizonTalAdalVance()返回不一致的结果
我正在研究一个函数,该函数可以根据字符串中的最宽字符确定可以适合给定宽度的最大字符数(以像素为单位),然后相应地划定字符串。我正在使用qfontmetrics的水平趋势方法,我一直在注意到一些不一致之处。
以以下代码为例:
QFont myFont("Times New Roman", 12);
QFontMetrics fm(myFont);
qDebug() << "Width of 'w': " << fm.horizontalAdvance('w');
qDebug() << "Width of 'wwww': " << fm.horizontalAdvance("wwww");
输出
Width of 'w': 9
Width of 'wwww': 35
您希望第二个代码的宽度为36,对吗?有趣的是,在第二个字符串中添加一个“ w”产生:
Width of 'w': 9
Width of 'wwwww': 43
应该为45。水平弹力返回一个int,因为它以像素为单位,但看起来几乎好像在某个地方计算中丢失了十进制值。
这是一个错误还是我错过了什么?
I'm working on a function that determines the maximum number of characters that can fit in a given width (in pixels) based on the widest character in the string and then chops the string accordingly. I'm using the horizontalAdvance method of QFontMetrics and I've been noticing some inconsistencies.
Take the following code for instance:
QFont myFont("Times New Roman", 12);
QFontMetrics fm(myFont);
qDebug() << "Width of 'w': " << fm.horizontalAdvance('w');
qDebug() << "Width of 'wwww': " << fm.horizontalAdvance("wwww");
The output is
Width of 'w': 9
Width of 'wwww': 35
You would expect the width of the second one to be 36, right? Interestingly enough, adding one 'w' in the second string yields:
Width of 'w': 9
Width of 'wwwww': 43
Which should be 45. horizontalAdvance returns an int because it's measuring in pixels, but it almost looks like there's a decimal value being lost in the calculation somewhere.
Is this a bug or am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论