from PyQt4 import QtGui font = QtGui.QFont("Arial", 10) font_metrics = QtGui.QFontMetrics(font) text = "Hello, world!" width = font_metrics.width(text) print("Width of text:", width)
from PyQt4 import QtGui font_metrics = QtGui.QFontMetrics(QtGui.QFont()) text = "Hello, world!" height = font_metrics.height() print("Height of text:", height)In both examples, we utilize the QFontMetrics class to measure the text and then print the result to the console. The PyQt4 package library is used to import the necessary modules for the examples.