Skip to content

Instantly share code, notes, and snippets.

@rsgalloway
Created March 12, 2014 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rsgalloway/9514597 to your computer and use it in GitHub Desktop.
Save rsgalloway/9514597 to your computer and use it in GitHub Desktop.
PyQt Elided QLabel
import sys
from PyQt4.QtCore import Qt
from PyQt4.QtGui import *
class ElidedLabel(QLabel):
def paintEvent(self, event):
painter = QPainter(self)
metrics = QFontMetrics(self.font())
elided = metrics.elidedText(self.text(), Qt.ElideRight, self.width())
painter.drawText(self.rect(), self.alignment(), elided)
if __name__ == '__main__':
app = QApplication(sys.argv)
main = QWidget()
main.setFixedHeight(50)
text = """
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.
""".replace("\n", "")
label = ElidedLabel()
label.setText(text)
label.setWindowFlags(Qt.Dialog)
layout = QHBoxLayout()
layout.setSizeConstraint(QLayout.SetNoConstraint)
layout.addWidget(label)
main.setLayout(layout)
main.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment