def paint(self, painter, option, index): """Paint a toolbutton with an icon.""" super(ToolButtonDelegate, self).paint(painter, option, index) opt = QStyleOptionToolButton() opt.rect = self.get_btn_rect(option) opt.iconSize = icons.get_iconsize('small') opt.state |= QStyle.State_Enabled | QStyle.State_Raised opt.icon = icons.get_icon('erase-right') QApplication.style().drawControl(QStyle.CE_ToolButtonLabel, opt, painter)
def drawComplexControl( self, cc: QStyle.ComplexControl, opt: QStyleOptionComplex, p: QPainter, widget: QWidget, ): if cc == QStyle.CC_ToolButton: toolbutton = opt if toolbutton: button = super().subControlRect( cc, toolbutton, QStyle.SC_ToolButton, widget ) menuarea = super().subControlRect( cc, toolbutton, QStyle.SC_ToolButtonMenu, widget ) bflags = toolbutton.state & ~QStyle.State_Sunken # type: ignore if bflags & QStyle.State_AutoRaise: if not (bflags & QStyle.State_MouseOver) or ( not (bflags & QStyle.State_Enabled) ): bflags &= ~QStyle.State_Raised mflags = bflags if toolbutton.state & QStyle.State_Sunken: # type: ignore if toolbutton.activeSubControls & QStyle.SC_ToolButton: # type: ignore bflags |= QStyle.State_Sunken mflags |= QStyle.State_Sunken tool = QStyleOption() tool.palette = toolbutton.palette if toolbutton.subControls & QStyle.SC_ToolButton: # type: ignore if bflags & ( QStyle.State_Sunken | QStyle.State_On | QStyle.State_Raised ): tool.rect = button tool.state = bflags # type: ignore self.proxy().drawPrimitive( QStyle.PE_PanelButtonTool, tool, p, widget ) if toolbutton.state & QStyle.State_HasFocus: # type: ignore fr = QStyleOptionFocusRect(toolbutton) # type: ignore fr.state = toolbutton.state fr.direction = toolbutton.direction fr.rect = QtCore.QRect(toolbutton.rect) fr.fontMetrics = toolbutton.fontMetrics fr.palette = toolbutton.palette fr.rect.adjust(3, 3, -3, -3) if toolbutton.features & QStyleOptionToolButton.MenuButtonPopup: # type: ignore fr.rect.adjust( 0, 0, -self.proxy().pixelMetric( QStyle.PM_MenuButtonIndicator, toolbutton, widget ), 0, ) self.proxy().drawPrimitive( QStyle.PE_FrameFocusRect, fr, p, widget ) label = QStyleOptionToolButton(toolbutton) # type: ignore label.state = bflags # type: ignore fw = self.proxy().pixelMetric(QStyle.PM_DefaultFrameWidth, opt, widget) label.rect = button.adjusted(fw, fw, -fw, -fw) self.proxy().drawControl(QStyle.CE_ToolButtonLabel, label, p, widget) if toolbutton.subControls & QStyle.SC_ToolButtonMenu: # type: ignore tool.rect = menuarea tool.state = mflags # type: ignore if mflags & ( QStyle.State_Sunken | QStyle.State_On | QStyle.State_Raised ): self.proxy().drawPrimitive( QStyle.PE_IndicatorButtonDropDown, tool, p, widget ) self.proxy().drawPrimitive( QStyle.PE_IndicatorArrowDown, tool, p, widget ) elif toolbutton.features & QStyleOptionToolButton.HasMenu: # type: ignore mbi = self.proxy().pixelMetric( QStyle.PM_MenuButtonIndicator, toolbutton, widget ) ir = toolbutton.rect new_toolbutton = toolbutton new_toolbutton.rect = QtCore.QRect( int(ir.center().x() + 1 - (mbi - 6) / 2), ir.y() + ir.height() - mbi + 4, mbi - 6, mbi - 6, ) self.proxy().drawPrimitive( QStyle.PE_IndicatorArrowDown, new_toolbutton, p, widget ) return return super(TTToolButtonStyle, self).drawComplexControl(cc, opt, p, widget)