def paint(self, painter, option, index): """ Paint a checkbox without the label. """ checked = bool(index.data()) check_box_style_option = QtWidgets.QStyleOptionButton() isEnabled = (index.flags() & QtCore.Qt.ItemIsEditable) > 0 and ( index.flags() & QtCore.Qt.ItemIsEnabled) > 0 if isEnabled: check_box_style_option.state |= QtWidgets.QStyle.State_Enabled else: check_box_style_option.state |= QtWidgets.QStyle.State_ReadOnly if checked: check_box_style_option.state |= QtWidgets.QStyle.State_On else: check_box_style_option.state |= QtWidgets.QStyle.State_Off check_box_style_option.rect = self.getCheckBoxRect(option) check_box_style_option.state |= QtWidgets.QStyle.State_Enabled QtWidgets.QApplication.style().drawControl( QtWidgets.QStyle.CE_CheckBox, check_box_style_option, painter) model = index.model() color = model.data(index, constants.editChangedRole) if color is None: return drawRect(painter, option, color)
def getCheckBoxRect(self, option): check_box_style_option = QtWidgets.QStyleOptionButton() check_box_rect = QtWidgets.QApplication.style().subElementRect( QtWidgets.QStyle.SE_CheckBoxIndicator, check_box_style_option, None) check_box_point = QtCore.QPoint( option.rect.x() + option.rect.width() / 2 - check_box_rect.width() / 2, option.rect.y() + option.rect.height() / 2 - check_box_rect.height() / 2) return QtCore.QRect(check_box_point, check_box_rect.size())