def paintEvent(self, event): painter = QStylePainter(self) rect = self._subControlRect(QStyle.SC_SliderGroove) is_horizontal = self.orientation() == Qt.Horizontal minpos, maxpos = self.minimumPosition(), self.maximumPosition() span = rect.width() if is_horizontal else rect.height() x1 = QStyle.sliderPositionFromValue( self.minimum(), self.maximum(), minpos, span, self.invertedAppearance()) x2 = QStyle.sliderPositionFromValue( self.minimum(), self.maximum(), maxpos, span, self.invertedAppearance()) # Background painter.fillRect(rect, Qt.white) # Highlight painter.setOpacity(.7) if is_horizontal: painter.fillRect(x1, rect.y(), x2 - x1, rect.height(), Qt.yellow) else: painter.fillRect(rect.x(), x1, rect.width(), x2 - x1, Qt.yellow) painter.setOpacity(1) # Histogram if self._pixmap: painter.drawPixmap(rect, self._pixmap, self._pixmap.rect()) # Frame painter.setPen(QPen(QBrush(Qt.darkGray), 2)) painter.drawRect(rect) # Handles painter.setPen(QPen(QBrush(self._HANDLE_COLOR), self._HANDLE_WIDTH)) painter.setOpacity(9) if is_horizontal: painter.drawLine(x1, rect.y(), x1, rect.y() + rect.height()) painter.drawLine(x2, rect.y(), x2, rect.y() + rect.height()) else: painter.drawLine(rect.x(), x1, rect.x() + rect.width(), x1) painter.drawLine(rect.x(), x2, rect.x() + rect.width(), x2) painter.setOpacity(1) if self._show_text: painter.setFont(QFont('sans-serif', 7, QFont.Bold)) strMin, strMax = self.formatValues(minpos, maxpos) widthMin = painter.fontMetrics().width(strMin) widthMax = painter.fontMetrics().width(strMax) height = painter.fontMetrics().height() is_enough_space = x2 - x1 > 2 + (max(widthMax, widthMin) if is_horizontal else (2 * height + self._HANDLE_WIDTH)) if is_enough_space: if is_horizontal: painter.drawText(x1 + 3, rect.y() + height - 2, strMin) painter.drawText(x2 - widthMax - 1, rect.y() + rect.height() - 2, strMax) else: painter.drawText(rect.x() + 1, x1 + height, strMin) painter.drawText(rect.x() + rect.width() - widthMax - 1, x2 - 2, strMax)
def paintEvent(self, event): QLineEdit.paintEvent(self, event) if not self.text() and self.placeholderText() and \ not self.hasFocus(): p = QStylePainter(self) font = self.font() metrics = QFontMetrics(font) p.setFont(font) color = self.palette().color(QPalette.Mid) p.setPen(color) left, top, right, bottom = self.getTextMargins() contents = self.contentsRect() contents = contents.adjusted(left, top, -right, -bottom) text = metrics.elidedText(self.placeholderText(), Qt.ElideMiddle, contents.width()) p.drawText(contents, Qt.AlignLeft | Qt.AlignVCenter, text)