Created
March 12, 2014 19:35
-
-
Save rsgalloway/9514597 to your computer and use it in GitHub Desktop.
PyQt Elided QLabel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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