from PyQt5.QtGui import QFont, QFontMetrics font = QFont('Helvetica', 12) fm = QFontMetrics(font) text = "Hello, World!" width = fm.width(text) height = fm.height() rect = fm.boundingRect(text) print("Width:", width) print("Height:", height) print("Bounding Rectangle:", rect)
from PyQt5.QtGui import QFontMetricsF fm = QFontMetricsF(QFont('Helvetica', 12)) text = "Hello, World!" width = fm.width(text) height = fm.height() print("Width:", width) print("Height:", height)This example is similar to the previous one, but it uses the QFontMetricsF class instead of QFontMetrics. This class returns floating-point values instead of integers, which can be useful for some applications. Overall, both of these examples demonstrate the basic usage of QFontMetrics in PyQt.