class SimpleButton(QAbstractButton): """ A simple icon button widget. """ def __init__(self, parent=None, **kwargs): super().__init__(parent, **kwargs) self.__focusframe = None def focusInEvent(self, event): # reimplemented event.accept() self.__focusframe = QFocusFrame(self) self.__focusframe.setWidget(self) def focusOutEvent(self, event): # reimplemented event.accept() self.__focusframe.deleteLater() self.__focusframe = None def sizeHint(self): # reimplemented self.ensurePolished() iconsize = self.iconSize() icon = self.icon() if not icon.isNull(): iconsize = icon.actualSize(iconsize) return iconsize def minimumSizeHint(self): # reimplemented return self.sizeHint() 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)
class SimpleButton(QAbstractButton): """ A simple icon button widget. """ def __init__(self, parent=None, **kwargs): super().__init__(parent, **kwargs) self.__focusframe = None def focusInEvent(self, event): # reimplemented event.accept() self.__focusframe = QFocusFrame(self) self.__focusframe.setWidget(self) def focusOutEvent(self, event): # reimplemented event.accept() self.__focusframe.deleteLater() self.__focusframe = None def sizeHint(self): # reimplemented self.ensurePolished() iconsize = self.iconSize() icon = self.icon() if not icon.isNull(): iconsize = icon.actualSize(iconsize) return iconsize def minimumSizeHint(self): # reimplemented return self.sizeHint() 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 focusInEvent(self, event): # reimplemented event.accept() self.__focusframe = QFocusFrame(self) self.__focusframe.setWidget(self)