qt - PySide/PyQt truncate text in QLabel based on minimumSize -


I'm thinking that best minimizing the text in a QLB based on the lowest width / height Incoming Text Anyone It may also be a length, but to keep a clean layout I would like to filter the long wings to fill the maximum amount of space (the maximum width / height of the widget).

Example:

  'A very long string where there should be only one small one, but I can not control the input of the widget because it is a given value of a user'   

will be:

  'A very long string where only one should be small, but ...' needs the current font based on the required location is.  

How can I get it?

Here is a simple example of what I am, although this word is based on calculation, not available space:

  PySide.QtCore import * Def truncateText from PySide .qtgui import * import sys (text): maxWords = 10 words = text.split ('') return '' .join (word [: maxwords]) + '...' application = QApplication (sys.argv) MainWindow = QWidget () layout = QHBoxLayout () mainWindow.setLayout (layout) text = 'this is a very long string' * 10 labels = Q label (truncateText (text)) label.setWordWrap (true) label.setFixedWidth (200) layout .addWidget (label) mainWindow.show () sys.exit (app.exec_ ())    

Even easier to use - QFontMetrics.elidedText method and paintEvent overload, here is an example:

 , \ QLabel, \ QFontMetrics, \ QPainter class mylabel (QLabel): def paintEvent (self, event): painter = QPainter (self) matrix = QFontMetrics (self.font ()) Elided = metrics. ElidedText (self.text (), Qt.ElideRight, self.width ()) painter.drawText (self.rect (), self.alignment (), elided) if (__name__ == '__main__'): app = none Does not (QApplication.instance ()): App = QApplication ([]) label = mylabel () label.setText ('this one In Astw formatted long and poorly runon used phrase to illustrate a point ') Label.setWindowFlags (Qt.Dialog) label.show () if (app): app.exec_ ()  < / Pre>  

Comments

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -