[PyQt] QFontMetrics.elidedText doesn't seem to be working

Roshan Mathews roshan at tachyontech.net
Mon Jun 15 06:39:03 BST 2009


The code below should elide the text in a label if it is longer
than it's width, and show it with ellipses (...) but it doesn't
work -- the line `msg = fm.elidedText(...' returns an empty
QString.

Any ideas why?

# begin code

from PyQt4 import QtGui, QtCore
import sys

app = QtGui.QApplication(sys.argv)

class ElidingLabel(QtGui.QLabel):
     def __init__(self, text="", parent=None):
         super(ElidingLabel, self).__init__(text, parent)
         self.msg = text

     def resizeEvent(self, event):
         width = event.size().width()
         fm = self.fontMetrics()
         msg = fm.elidedText(self.msg, width,
                             QtCore.Qt.ElideRight)
         print repr(self.msg), repr(msg), width
         self.setText(msg)

label = ElidingLabel("Lorem ipsum dolor sit amet,"
                      " consectetur cras amet.")
label.show()

sys.exit(app.exec_())

# end code

Thanks,
Roshan Mathews


More information about the PyQt mailing list