def editorEvent( self, event: QtCore.QEvent, model: QtCore.QAbstractItemModel, option: QStyleOptionViewItem, index: QtCore.QModelIndex, ) -> bool: """Called when an event has occured in the editor. This can be used to customize how the delegate handles mouse/key events """ if (event.type() == event.MouseButtonRelease and event.button() == Qt.RightButton): self.show_context_menu(index, model, event.globalPos(), option.widget) # if the user clicks quickly on the visibility checkbox, we *don't* # want it to be interpreted as a double-click. We want the visibilty # to simply be toggled. if event.type() == event.MouseButtonDblClick: self.initStyleOption(option, index) style = option.widget.style() check_rect = style.subElementRect( style.SE_ItemViewItemCheckIndicator, option, option.widget) if check_rect.contains(event.pos()): cur_state = index.data(Qt.CheckStateRole) if model.flags(index) & Qt.ItemIsUserTristate: state = Qt.CheckState((cur_state + 1) % 3) else: state = Qt.Unchecked if cur_state else Qt.Checked return model.setData(index, state, Qt.CheckStateRole) # refer all other events to the QStyledItemDelegate return super().editorEvent(event, model, option, index)
def flags(self, index): """Read-only.""" if not index.isValid(): return 0 return QAbstractItemModel.flags(self, index)
def flags(self, index): """Override to make cells editable.""" if not index.isValid(): return Qt.ItemIsEnabled return QAbstractItemModel.flags(self, index)