def paint(self, painter, option, index): painter.save() checked = index.model().data(index, Qt.UserRole) checkbox_indicator = QStyleOptionButton() #checkbox_indicator.state |= QStyle.State_Enabled if (index.flags() & Qt.ItemIsEditable) > 0: checkbox_indicator.state |= QStyle.State_Enabled else: checkbox_indicator.state |= QStyle.State_ReadOnly if checked: checkbox_indicator.state |= QStyle.State_On else: checkbox_indicator.state |= QStyle.State_Off checkbox_indicator.rect = QApplication.style().subElementRect( QStyle.SE_CheckBoxIndicator, checkbox_indicator, None) x = int(option.rect.center().x() - checkbox_indicator.rect.width() / 2) y = int(option.rect.center().y() - checkbox_indicator.rect.height() / 2) checkbox_indicator.rect.moveTo(x, y) if (option.state & QStyle.State_Selected): painter.fillRect(option.rect, option.palette.highlight()) QApplication.style().drawControl(QStyle.CE_CheckBox, checkbox_indicator, painter) painter.restore()
def paintSection(self, painter, rect, logicalIndex): painter.save() super(SelectableTableHeader, self).paintSection(painter, rect, logicalIndex) painter.restore() if logicalIndex in self._nonSelectableIndexes: return # check if boolean for this section exists if not self._isSectionSelected.has_key(logicalIndex): if self._selectionModel: if self.orientation() == Qt.Horizontal: self._isSectionSelected[ logicalIndex] = self._selectionModel.getColSelectionState( logicalIndex) elif self.orientation() == Qt.Vertical: self._isSectionSelected[ logicalIndex] = self._selectionModel.getRowSelectionState( logicalIndex) else: self._isSectionSelected[logicalIndex] = Qt.Checked option = QStyleOptionButton() option.rect = rect if self._isSectionSelected[logicalIndex] == Qt.Checked: option.state = QStyle.State_On | QStyle.State_Enabled elif self._isSectionSelected[logicalIndex] == Qt.Unchecked: option.state = QStyle.State_Off | QStyle.State_Enabled elif self._isSectionSelected[logicalIndex] == Qt.PartiallyChecked: option.state = QStyle.State_NoChange | QStyle.State_Enabled self.style().drawControl(QStyle.CE_CheckBox, option, painter)
def checkBoxRect(self, opt, editor): cb_option = QStyleOptionButton() style = QApplication.style() cb_rect = style.subElementRect(QStyle.SE_CheckBoxIndicator, cb_option, editor) cb_point = QPoint( opt.rect.x() + (opt.rect.width() - cb_rect.width()) / 2, opt.rect.y() + (opt.rect.height() - cb_rect.height()) / 2) return QRect(cb_point, cb_rect.size())
def getCheckBoxRect(self, option): check_box_style_option = QStyleOptionButton() check_box_rect = QApplication.style().subElementRect( QStyle.SE_CheckBoxIndicator, check_box_style_option, None) check_box_point = 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 QRect(check_box_point, check_box_rect.size())
def paintSection(self, painter, rect, logicalIndex): #painter.save() #QHeaderView.paintSection(self, painter, rect, logicalIndex) #painter.restore() if logicalIndex == 0: option = QStyleOptionButton() option.rect = QRect(1, 10, 10, 10) if self.isOn: option.state = QStyle.State_On else: option.state = QStyle.State_Off self.style().drawControl(QStyle.CE_CheckBox, option, painter)
def paint(self, painter, option, index): ''' Paint a checkbox without the label. ''' value = index.model().data(index, Qt.DisplayRole) if value == None: return # we draw the background cg = QPalette.Disabled if option.state & QStyle.State_Enabled: cg = QPalette.Normal if not (option.state & QStyle.State_Active): cg = QPalette.Inactive r = QRect(option.rect.x(),option.rect.y(),option.rect.width(),option.rect.height()) if option.state & QStyle.State_Selected: painter.fillRect(option.rect, option.palette.color(cg, QPalette.Highlight)) elif index.data(Qt.BackgroundRole): # Alternating background color for the rows, to improve # readability # if index.row() % 2 == 1: # painter.fillRect(option.rect, QColor(240,240,255)) painter.fillRect(option.rect, index.data(Qt.BackgroundRole)) else: painter.fillRect(option.rect, Qt.GlobalColor.white) checked = bool(index.model().data(index, Qt.DisplayRole)) check_box_style_option = QStyleOptionButton() if (index.flags() & Qt.ItemIsEditable) > 0: check_box_style_option.state |= QStyle.State_Enabled else: check_box_style_option.state |= QStyle.State_ReadOnly if checked: check_box_style_option.state |= QStyle.State_On else: check_box_style_option.state |= QStyle.State_Off check_box_style_option.rect = self.getCheckBoxRect(option) # if not index.model().hasFlag(index, Qt.ItemIsEditable): if not ((index.flags() & Qt.ItemIsEditable) > 0): check_box_style_option.state |= QStyle.State_ReadOnly QApplication.style().drawControl(QStyle.CE_CheckBox, check_box_style_option, painter)
def paint(self, painter, option, index): checked = bool(index.data()) check_box_style_option = QStyleOptionButton() if (index.flags() & Qt.ItemIsEditable) > 0: check_box_style_option.state |= QStyle.State_Enabled else: check_box_style_option.state |= QStyle.State_ReadOnly if checked: check_box_style_option.state |= QStyle.State_On else: check_box_style_option.state |= QStyle.State_Off check_box_style_option.rect = self.getCheckBoxRect(option) check_box_style_option.state |= QStyle.State_Enabled QApplication.style().drawControl(QStyle.CE_CheckBox, check_box_style_option, painter)
def paintEvent(self, e): p = QPainter(self) style = QApplication.style() option = QStyleOptionButton() style.drawControl(QStyle.CE_PushButton, option, p) self._painted = True