edit: when change main window size, need cut text of qlabel()
includes in qstatusbar()
fit size of form. shown in diagram below.
from pyqt4.qtgui import * pyqt4.qtcore import * import sys class mymainwindow(qmainwindow): def __init__(self): qmainwindow.__init__(self) self.setcentralwidget(qlabel("main widget")) statusbar = qstatusbar() self.setstatusbar(statusbar) statusbar.addwidget(qlabel("first message")) statusbar.addwidget(qlabel("second message")) statusbar.addwidget(qlabel("a thrid long long long long long message")) app = qapplication(sys.argv) window = mymainwindow() window.show() sys.exit(app.exec_())
the key set appropriate minimum width on labels, allow them resize smaller width of text. however, crop text on right, rather eliding (i.e. ...
appears @ end).
the blog post status bar , elided label explains how elided text, solution written in c++. below equivalent version written in pyqt4:
import sys pyqt4 import qtcore, qtgui class elidedlabel(qtgui.qlabel): _width = _text = _elided = none def __init__(self, text='', width=40, parent=none): super(elidedlabel, self).__init__(text, parent) self.setminimumwidth(width if width > 0 else 1) def elidedtext(self): return self._elided or '' def paintevent(self, event): painter = qtgui.qpainter(self) self.drawframe(painter) margin = self.margin() rect = self.contentsrect() rect.adjust(margin, margin, -margin, -margin) text = self.text() width = rect.width() if text != self._text or width != self._width: self._text = text self._width = width self._elided = self.fontmetrics().elidedtext( text, qtcore.qt.elideright, width) option = qtgui.qstyleoption() option.initfrom(self) self.style().drawitemtext( painter, rect, self.alignment(), option.palette, self.isenabled(), self._elided, self.foregroundrole()) class mymainwindow(qtgui.qmainwindow): def __init__(self): super(qtgui.qmainwindow, self).__init__() self.setcentralwidget(qtgui.qlabel("main widget")) statusbar = qtgui.qstatusbar() self.setstatusbar(statusbar) statusbar.addwidget(elidedlabel("first message")) statusbar.addwidget(elidedlabel("second message")) statusbar.addwidget(elidedlabel("a third long long long long long message")) app = qtgui.qapplication(sys.argv) window = mymainwindow() window.show() sys.exit(app.exec_())
Comments
Post a Comment