Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Incorrect character width calculation

See original GitHub issue


In the character width is calculated using boundingRect. In my system (Windows 10, python 3.8.5, pyqt5 5.15.0) the result of boundingRect width() is not consistent (even though the font is monospaced).

print(' ', font_metrics.boundingRect(' ').width(), font_metrics.horizontalAdvance(' '), file=f)
for c in range(ord('a'), ord('z') + 1):
  print(chr(c), font_metrics.boundingRect(chr(c)).width(), font_metrics.horizontalAdvance(chr(c)), file=f)
  1 6 <- ' ' character
a 5 6
b 6 6
c 5 6
d 5 6
e 5 6
f 5 6
g 5 6
h 5 6
i 5 6
j 4 6
k 6 6
l 5 6
m 6 6
n 5 6
o 6 6
p 6 6
q 5 6
r 5 6
s 5 6
t 5 6
u 5 6
v 6 6
w 6 6
x 5 6
y 6 6
z 5 6

This results in the wrong window width. What works is to use horizontalAdvance which is the alternative to the deprecated width method (

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

char101commented, Aug 28, 2020

@yannick1974 Thanks, I have updated the pull request.

yannick1974commented, Aug 28, 2020


I think it’s worth replacing the other boundingRect. On my system, for a reason I don’t understand, boundingRect(' ').width() returns 0 (I tried with different fonts). This leads to a ZeroDivisionError at that makes qtconsole crash when using pyqt5 (but not with pyside). Also, another effect is the qtconsole does not honour c.JupyterWidget.console_width .

I replaced all the boundingRect(' ').width() by horizontalAdvance(' ') and now everything works as expected.

Read more comments on GitHub >

github_iconTop Results From Across the Web

character width calculation wrong with some font which can ...
You have to ensure the font is properly loaded on the canvas before creating a fabricObject that uses it, if not fabricjs creates...
Read more >
STR #3364: Fl_Text_Display: wrong text width calculation
There is something wrong with text width calculation in Fl_Text_Display. ... look at the '/' characters at line ends => they miss a...
Read more >
How to properly calculate text size in PIL images
size_width and size_height would return incorrect values. The solution. So, the solution is to use different calculations for the the text size.
Read more >
How to Tune Typography Based on Characters Per Line
The width factor from the equation above tells you how many increments of width are needed to reach the desired CPL. Once you...
Read more >
Incorrect Font Metrics | Qt Forum
boundingRect(text).size(). Problem When using the Freestyle Script font the bounding box calculation performed by QFontMetricsF is incorrect ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found