简体   繁体   English

使用 PySide6 的 QFontMetrics 宽度

[英]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'它适用于 PyQt5,但我试图将代码移动到 PySide6,当我这样做时出现错误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.我试过阅读 QFontMetrics 文档,但我所做的一切似乎都给出了同样的错误。 Any ideas of how I can convert this line to PySide6?关于如何将此行转换为 PySide6 的任何想法? Thank you!谢谢!

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. 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 .正如文档(已经在 Qt5 的过时成员页面中)报告的那样, width()的结果在许多情况下不一致且不可靠,主要是因为它没有考虑字母轴承

You should use horizontalAdvance() or boundingRect() .width() .您应该使用 HorizontalAdvance( horizontalAdvance()boundingRect() .width()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM