def dispatchMouseEventToScene(self, event): if event.type() == QEvent.MouseButtonPress: newtype = QEvent.GraphicsSceneMousePress elif event.type() == QEvent.MouseButtonRelease: newtype = QEvent.GraphicsSceneMouseRelease else: raise ValueError('Unknown event type {}'.format(event.type())) # pulled from QGraphicsView::mousePressEvent in the qt codebase: mouseEvent = QGraphicsSceneMouseEvent(newtype) mousePressViewPoint = event.pos() mousePressScenePoint = self.mapToScene(mousePressViewPoint) mousePressScreenPoint = event.globalPos() lastMouseMoveScenePoint = mousePressScenePoint lastMouseMoveScreenPoint = mousePressScreenPoint mousePressButton = event.button() #mouseEvent.setWidget(self.viewport()) # TODO figure out how to do this in python, or if it's really necessary mouseEvent.setButtonDownScenePos(mousePressButton, mousePressScenePoint) mouseEvent.setButtonDownScreenPos(mousePressButton, mousePressScreenPoint) mouseEvent.setScenePos(mousePressScenePoint) mouseEvent.setScreenPos(mousePressScreenPoint) mouseEvent.setLastScenePos(lastMouseMoveScenePoint) mouseEvent.setLastScreenPos(lastMouseMoveScreenPoint) mouseEvent.setButtons(event.buttons()) mouseEvent.setButton(event.button()) mouseEvent.setModifiers(event.modifiers()) mouseEvent.setSource(event.source()) mouseEvent.setFlags(event.flags()) mouseEvent.setAccepted(False) QApplication.sendEvent(self.scene(), mouseEvent) return mouseEvent
def mouseDoubleClickEvent(self, event): if self.textInteractionFlags() == Qt.TextEditorInteraction: super(CustomQGraphicsTextItem, self).mouseDoubleClickEvent(event) return self.setTextInteractionFlags(Qt.TextEditorInteraction) self.setFocus(Qt.MouseFocusReason) click = QGraphicsSceneMouseEvent(QEvent.GraphicsSceneMousePress) click.setButton(event.button()) click.setPos(event.pos()) self.mousePressEvent(click)