我需要获取某些HTMLQLabel包含的长度(以像素为单位):QLabel
主要.py:
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QHBoxLayout
class Win(QWidget):
def __init__(self):
super().__init__()
self.label = QLabel('<font style = "color:red">а</font>')
self.l = QHBoxLayout()
self.l.addWidget(self.label)
self.setLayout(self.l)
self.show()
print(self.label.fontMetrics().boundingRect(self.label.text()).width())
if __name__ == '__main__':
app = QApplication([])
win = Win()
app.exec_()
代码返回200像素,显然更多。
如何解决这个问题呢?如果可能的话,请告诉我如何获取不带标签的文本字符长度



