def paintEvent(self, event): super(PromptLineEdit, self).paintEvent(event) qt_api = os.environ['QT_API'].lower() if self._prompt_text and not self.text() and self.isEnabled(): if qt_api in PYQT4_API: from PyQt4.QtGui import QStyleOptionFrameV3 option = QStyleOptionFrameV3() elif qt_api in PYSIDE_API: from PySide.QtGui import QStyleOptionFrameV3 option = QStyleOptionFrameV3() elif qt_api in PYQT5_API: from PyQt5.QtWidgets import QStyleOptionFrame option = QStyleOptionFrame() else: msg = 'Qt bindings "%s" is not supported' % qt_api raise PythonQtError(msg) self.initStyleOption(option) left, top, right, bottom = self.getTextMargins() va = self.style().visualAlignment(self.layoutDirection(), self.alignment()) rect = self.style().subElementRect( QtWidgets.QStyle.SE_LineEditContents, option, self).adjusted(2, 0, 0, 0).adjusted(left, top, -right, -bottom) fm = QtGui.QFontMetrics(self.font()) text = fm.elidedText(self._prompt_text, QtCore.Qt.ElideRight, rect.width()) painter = QtGui.QPainter(self) painter.setPen(self.palette().color(QtGui.QPalette.Disabled, QtGui.QPalette.Text)) painter.drawText(rect, va, text)
def textRect(self, text): textSize = QtGui.QFontMetrics(self.font()).size(0, text) return QtCore.QRect(QtCore.QPoint(), textSize)