def sendEvent(self, event: QEvent) -> None: """ Send an event. Args: event: The event to send. """ if event.type() == QEvent.Type.FocusOut: # Ignored - cannot get focus back reliably :-( return if isinstance(event, QMouseEvent): # `windowPos` is replaced with `localPos` as it has no meaning in offscreen rendering. event = QMouseEvent(event.type(), event.localPos(), event.screenPos(), event.button(), event.buttons(), event.modifiers()) elif isinstance(event, QWheelEvent): event = QWheelEvent(event.position(), event.position(), event.pixelDelta(), event.angleDelta(), event.buttons(), event.modifiers(), event.phase(), event.inverted(), event.source()) self._window.contentItem().forceActiveFocus() QCoreApplication.sendEvent(self._window, event)
def editorEvent(self, event: QEvent, model: 'PropModel', option: QStyleOptionViewItem, index: QModelIndex) -> bool: """ Change the data in the model and the state of the checkbox if the user presses the left mouse button or presses Key_Space or Key_Select and this cell is editable. Otherwise do nothing. :param event: The event that will take place to trigger the editor Event. :type event: QEvent :param model: The model that our delegate will render. :type model: PropModel :param option: Option for the kind've event that takes place. :type option: QStyleOptionViewItem :param index: Index of the events. :type index: QModelIndex :return: true if the given editor is a valid QWidget and the given event is handled; otherwise returns false. :rtype: bool """ event.type() if not (index.flags() & QtCore.Qt.ItemIsEditable) > 0: return False data = index.internalPointer() if index.column() != 1 or not isinstance( data, Property) or data.getType() != bool: return QStyledItemDelegate.editorEvent(self, event, model, option, index) if data.isReadOnly(): return False # Do not change the checkbox-state if event.type() == QEvent.MouseButtonPress: return False if event.type() == QEvent.MouseButtonRelease or event.type( ) == QEvent.MouseButtonDblClick: if event.button( ) != QtCore.Qt.LeftButton or not self.getCheckBoxRect( option).contains(event.pos()): return False if event.type() == QEvent.MouseButtonDblClick: return True elif event.type() == QEvent.KeyPress: if event.key() != QtCore.Qt.Key_Space and event.key( ) != QtCore.Qt.Key_Select: return False else: return False # Change the checkbox-state checkbox = QCheckBox('temp') checkbox.setChecked(not data.getValue()) self.setModelData(checkbox, model, index) return True
def editorEvent(self, event: QEvent, model: QAbstractItemModel, option: QStyleOptionViewItem, index: QModelIndex) -> bool: if not (index.flags() & Qt.ItemIsEditable) > 0: return False if event.type() == QEvent.MouseButtonRelease: if event.button() != Qt.LeftButton: # or not self.__getCheckboxRect(option).contains(event.pos()): return False if event.type() == QEvent.MouseButtonDblClick: return True elif event.type() == QEvent.KeyPress: if event.key() != Qt.Key_Space and event.key() != Qt.Key_Select: return False else: return False self.setModelData(None, model, index) return True # super().editorEvent(event, model, option, index)