Skip to content

Port away from deprecated QFontMetrics::width()

Elvis Angelaccio requested to merge work/widthDeprecated into master

While the documention says to port to QFontMetrics::horizontalAdvance(), what we actually need is not the horizontal advance, but the width of the text. So we need to port to QFontMetrics::boundingRect().width().

Quoting from https://kdepepo.wordpress.com/2019/08/05/about-deprecation-of-qfontmetricswidth/:

"Since it was not clear from the confusingly named function QFontMetrics::width() that it actually returned the horizontal advance, instead of the bounding width, this method is now obsolete. You must port to either QFontMetrics::horizontalAdvance() or QFontMetrics::boundingRect().width().

Please make sure you are aware of the difference, and do not port blindly. I am pretty sure that in most cases QFontMetrics::boundingRect() is what you want, unless you are writing custom text shaping/layouting code. Using the wrong function can cause clipped text or text that suddenly wraps to the next line despite calculating the width that it needs."

Merge request reports