Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QFontMetrics Width using PySide6

I have the following line in my code:

self.textLength = self.fontMetrics().width(self.text())

It works with PyQt5, but I'm trying to move the code to PySide6, and when I do I get the error AttributeError: type object 'PySide6.QtGui.QFontMetrics' has no attribute 'width'

I've tried reading through the QFontMetrics docs, but everything I do seems to give the same error. Any ideas of how I can convert this line to PySide6? Thank you!

like image 412
Ricky Kresslein Avatar asked Jun 04 '26 23:06

Ricky Kresslein


1 Answers

QFontMetrics.width() has been considered obsolete since Qt 5.5 and deprecated from Qt 5.11 (but will probably be still supported throughout any future releases of Qt 5), and eventually removed in Qt 6.

As the documentation (which already is in the obsolete members page of Qt5) reports, the results of width() were inconsistent and unreliable in many situations, mostly because it didn't consider the letter bearings.

You should use horizontalAdvance() or boundingRect().width().

like image 200
musicamante Avatar answered Jun 07 '26 20:06

musicamante