def paintEvent(self, event): super().paintEvent(event) painter = QStylePainter(self) opt = QStyleOptionFrame() opt.initFrom(self) painter.drawPrimitive(QStyle.PE_FrameDockWidget, opt) painter.end()
def paintEvent(self, event): if self.__flat: opt = QStyleOptionToolButton() self.initStyleOption(opt) p = QStylePainter(self) p.drawControl(QStyle.CE_ToolButtonLabel, opt) else: QToolButton.paintEvent(self, event)
def paintEvent(self, event): p = QStylePainter(self) opt = QStyleOptionToolButton() self.initStyleOption(opt) if self.__text: # Replace the text opt.text = self.__text p.drawComplexControl(QStyle.CC_ToolButton, opt) p.end()
def paintEvent(self, event): """Reimplemented.""" opt = QStyleOptionComboBox() self.initStyleOption(opt) painter = QStylePainter(self) painter.drawComplexControl(QStyle.CC_ComboBox, opt) if not self.__searchline.isVisibleTo(self): opt.editable = False painter.drawControl(QStyle.CE_ComboBoxLabel, opt)
def paintEvent(self, event): painter = QStylePainter(self) option = QStyleOption() option.initFrom(self) rect = option.rect metrics = option.fontMetrics text = metrics.elidedText(self.__text, self.__textElideMode, rect.width()) painter.drawItemText(rect, self.__alignment, option.palette, self.isEnabled(), text, self.foregroundRole()) painter.end()
def paintEvent(self, event): painter = QStylePainter(self) opt = QStyleOption() opt.initFrom(self) painter.drawPrimitive(QStyle.PE_Widget, opt) if not self.__icon.isNull(): rect = self.contentsRect() if opt.state & QStyle.State_Active: mode = QIcon.Active else: mode = QIcon.Disabled self.__icon.paint(painter, rect, Qt.AlignCenter, mode, QIcon.Off) painter.end()
def paintEvent(self, event): # reimplemented painter = QStylePainter(self) option = QStyleOptionButton() option.initFrom(self) option.icon = self.icon() option.iconSize = self.iconSize() icon = self.icon() if not icon.isNull(): if option.state & QStyle.State_Active: mode = (QIcon.Normal if option.state & QStyle.State_MouseOver else QIcon.Active) else: mode = QIcon.Disabled pixmap = icon.pixmap(option.iconSize, mode, ) painter.drawItemPixmap(option.rect, Qt.AlignCenter, pixmap)
def paintEvent(self, event): painter = QStylePainter(self) painter.setOpacity(self._opacity) opt = QStyleOption() opt.initFrom(self) painter.drawPrimitive(QStyle.PE_Widget, opt) if not self.__icon.isNull(): rect = self.contentsRect() if opt.state & QStyle.State_Active: mode = QIcon.Active else: mode = QIcon.Disabled self.__icon.paint(painter, rect, Qt.AlignCenter, mode, QIcon.Off) painter.end()
def paintEvent(self, event): opt = QStyleOptionToolButton() self.initStyleOption(opt) if self.__showMenuIndicator and self.isChecked(): opt.features |= QStyleOptionToolButton.HasMenu if self.__flat: # Use default widget background/border styling. StyledWidget_paintEvent(self, event) p = QStylePainter(self) p.drawControl(QStyle.CE_ToolButtonLabel, opt) else: p = QStylePainter(self) p.drawComplexControl(QStyle.CC_ToolButton, opt)
def paintEvent(self, event): # type: (QPaintEvent) -> None p = QStylePainter(self) opt = QStyleOptionToolButton() self.initStyleOption(opt) p.drawComplexControl(QStyle.CC_ToolButton, opt) p.end()
def paintEvent(self, event): # reimplemented painter = QStylePainter(self) option = QStyleOptionButton() option.initFrom(self) option.icon = self.icon() option.iconSize = self.iconSize() icon = self.icon() if not icon.isNull(): if option.state & QStyle.State_Active: mode = (QIcon.Normal if option.state & QStyle.State_MouseOver else QIcon.Active) else: mode = QIcon.Disabled pixmap = icon.pixmap( option.iconSize, mode, ) painter.drawItemPixmap(option.rect, Qt.AlignCenter, pixmap)
def eventFilter(o, e): if (isinstance(o, QAbstractButton) and e.type() == QEvent.Paint): # paint by hand (borrowed from QTableCornerButton) btn = o opt = QStyleOptionHeader() opt.initFrom(btn) state = QStyle.State_None if btn.isEnabled(): state |= QStyle.State_Enabled if btn.isActiveWindow(): state |= QStyle.State_Active if btn.isDown(): state |= QStyle.State_Sunken opt.state = state opt.rect = btn.rect() opt.text = btn.text() opt.position = QStyleOptionHeader.OnlyOneSection painter = QStylePainter(btn) painter.drawControl(QStyle.CE_Header, opt) return True # eat event return False
def eventFilter(self, o, e): if (isinstance(o, QAbstractButton) and e.type() == QEvent.Paint): # paint by hand (borrowed from QTableCornerButton) btn = o opt = QStyleOptionHeader() opt.initFrom(btn) state = QStyle.State_None if btn.isEnabled(): state |= QStyle.State_Enabled if btn.isActiveWindow(): state |= QStyle.State_Active if btn.isDown(): state |= QStyle.State_Sunken opt.state = state opt.rect = btn.rect() opt.text = btn.text() opt.position = QStyleOptionHeader.OnlyOneSection painter = QStylePainter(btn) painter.drawControl(QStyle.CE_Header, opt) return True # eat event return False
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)
def paintEvent(self, event): painter = QStylePainter(self) option = QStyleOptionButton() option.initFrom(self) option.text = "" option.icon = self.icon() option.iconSize = self.iconSize() option.features = QStyleOptionButton.Flat if self.isDown(): option.state |= QStyle.State_Sunken painter.drawPrimitive(QStyle.PE_PanelButtonBevel, option) if not option.icon.isNull(): if option.state & QStyle.State_Active: mode = (QIcon.Active if option.state & QStyle.State_MouseOver else QIcon.Normal) else: mode = QIcon.Disabled if self.isChecked(): state = QIcon.On else: state = QIcon.Off option.icon.paint(painter, option.rect, Qt.AlignCenter, mode, state)
def paintEvent(self, event): # based on # https://github.com/qt/qtbase/blob/f40dbe0d0b54ce83d2168e82905cf4f75059a841/src/widgets/widgets/qslider.cpp#L315 # https://github.com/enthought/traitsui/blob/master/traitsui/qt4/extra/range_slider.py painter = QStylePainter(self) minpos = self.__min_position maxpos = self.__max_position # Draw the groove opt = QStyleOptionSlider() self.initStyleOption(opt) # Draw empty grove opt.sliderPosition = opt.minimum opt.subControls = QStyle.SC_SliderGroove if self.tickPosition() != self.NoTicks: opt.subControls |= QStyle.SC_SliderTickmarks painter.drawComplexControl(QStyle.CC_Slider, opt) # Draw the highlighted part on top # Qt4.8 and Qt5.3 draw the highlighted groove in a weird way because they # transpose opt.rect. Qt5.7 works fine. if QT_VERSION_STR >= '5.7.0': opt.subControls = QStyle.SC_SliderGroove opt.sliderPosition = opt.maximum if self.orientation() == Qt.Horizontal: _w = opt.rect.width() / opt.maximum x = round(_w * minpos) w = round(_w * (maxpos - minpos)) opt.rect = QRect(x, 0, w, opt.rect.height()) else: _h = opt.rect.height() / opt.maximum y = round(_h * minpos) h = round(_h * (maxpos - minpos)) opt.rect = QRect(0, y, opt.rect.width(), h) painter.drawComplexControl(QStyle.CC_Slider, opt) # Draw the handles for i, position in enumerate((minpos, maxpos)): opt = QStyleOptionSlider() self.initStyleOption(opt) opt.subControls = QStyle.SC_SliderHandle if self.__pressed_control and (self.__active_slider == i or self.__active_slider < 0): opt.activeSubControls = self.__pressed_control opt.state |= QStyle.State_Sunken else: opt.activeSubControls = self.__hovered_control opt.sliderPosition = position opt.sliderValue = position painter.drawComplexControl(QStyle.CC_Slider, opt)
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)
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): 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('Monospace', 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 > 3 + (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, strMin) painter.drawText(x2 - widthMax - 2, rect.y() + rect.height() - 3, strMax) else: painter.drawText(rect.x() + 1, x1 + height, strMin) painter.drawText(rect.x() + rect.width() - widthMax - 1, x2 - 2, strMax) # Tooltip if self._showControlTooltip\ and (not self._show_text or not is_enough_space): # (show control-drag tooltip) painter.setFont(QFont('Monospace', 10, QFont.Normal)) text = "Hold {} to move time interval" \ .format("Cmd" if sys.platform == "darwin" else "Ctrl") w = painter.fontMetrics().width(text) h = painter.fontMetrics().height() brush = QColor(224, 224, 224, 212) pen = QPen(Qt.NoPen) rect = QRect(4, 4, w + 8, h + 4) painter.setBrush(brush) painter.setPen(pen) painter.drawRect(rect) painter.setPen(Qt.black) painter.drawText(8, 4 + h, text)
def paintEvent(self, event): # type: (QPaintEvent) -> None if self.__flat: opt = QStyleOptionToolButton() self.initStyleOption(opt) p = QStylePainter(self) p.drawControl(QStyle.CE_ToolButtonLabel, opt) p.end() else: super().paintEvent(event) # paint shadow shadow = innerShadowPixmap(self.__shadowColor, self.size(), self.__shadowPosition, length=self.__shadowLength) p = QPainter(self) rect = self.rect() targetRect = QRect(rect.left() + 1, rect.top() + 1, rect.width() - 2, rect.height() - 2) p.drawPixmap(targetRect, shadow, shadow.rect()) p.end()
def paintEvent(self, _event) -> None: painter = QStylePainter(self) option = QStyleOptionComboBox() self.initStyleOption(option) painter.drawComplexControl(QStyle.CC_ComboBox, option) foreground = self.currentData(Qt.ForegroundRole) if isinstance(foreground, (QBrush, QColor)): foreground = QBrush(foreground) if foreground.style() != Qt.NoBrush: # some styles take WindowText some just use current pen? option.palette.setBrush(QPalette.WindowText, foreground) option.palette.setBrush(QPalette.ButtonText, foreground) option.palette.setBrush(QPalette.Text, foreground) painter.setPen(QPen(foreground, painter.pen().widthF())) font = self.currentData(Qt.FontRole) if isinstance(font, QFont): option.fontMetrics = QFontMetrics(font) painter.setFont(font) painter.drawControl(QStyle.CE_ComboBoxLabel, option)
def paintEvent(self, e): #print("hover: ",self._hover,",press: ",self._press) painter = QStylePainter(self) # prepare the drawing options # for drawing slider groove opt = QStyleOptionSlider() opt.initFrom(self) opt.subControls = QStyle.SC_SliderGroove opt.maximum = self.__steps opt.minimum = 0 opt.orientation = self.orientation() opt.sliderPosition = self.__position0 opt.sliderValue = self.__value0 if self._tickList is not None: opt.tickPosition = QSlider.TicksBelow else: opt.tickPosition = QSlider.NoTicks handleWidth = self._handleWidth measureWidth = self.width() - handleWidth - handleWidth painter.drawComplexControl(QStyle.CC_Slider, opt) # draw tickmarks if self._tickList is not None: painter.drawPixmap(self._handleWidth, self.height() - self._tickHeight, self._tickImage) # handle colors colorPRESS = QColor(204, 204, 204) colorHOVER = QColor(23, 23, 23) colorNORMAL = QColor(0, 122, 217) if self.isEnabled() else colorPRESS handleHeight = self.height( ) if self._tickList is None else self.height() - self._tickHeight # draw handle 0 if self._press in {0, 2}: color = colorPRESS elif self._hover == 0: color = colorHOVER else: color = colorNORMAL self._rects[0] = r0 = QRect( self.__position0 / self.__steps * measureWidth, 0, handleWidth, handleHeight) painter.fillRect(self._rects[0], color) # draw handle 1 if self._press in {1, 2}: color = colorPRESS elif self._hover == 1: color = colorHOVER else: color = colorNORMAL self._rects[1] = r1 = QRect( self.__position1 / self.__steps * measureWidth + handleWidth, 0, handleWidth, handleHeight) painter.fillRect(self._rects[1], color) # draw inner handle if self._press == 2 or not self.isEnabled(): color = QColor(0, 0, 0, 15) elif self._hover == 2: color = QColor(0, 61, 108, 63) else: color = QColor(0, 122, 217, 63) self._rects[2] = QRect(r0.left(), r0.top(), r1.right() - r0.left() + 1, handleHeight) painter.fillRect(self._rects[2], color)
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)