Only move QGraphicsItem when mouse in specific reg

2019-05-21 04:55发布

问题:

I'm trying to create something similar to terragens node network view in python using PySide.
I subclassed QGraphicsRectItem using this code.

class Node(QGraphicsRectItem):
    def __init__(self,pos):
        QGraphicsRectItem.__init__(self,pos.x()-100,pos.y()-30,200,60)
        self.setFlag(QGraphicsItem.ItemIsMovable,True)
    (...)

Which gives this (with some fancy painting):

I'd like to implent connecting nodes by dragging the mouse from one small rectangle to another, but this results in moving the whole node.

So I don't want the QGraphicsRectItem getting moved when the mouse is pressed inside a small rectangle. How would I be able to do this.

(if needed, I can define something like isInDraggingArea(x,y))

Thanks in advance.

回答1:

I found the solution, sorry for wasting your time.

In the scene add this code:

def mousePressEvent(self, event):
    item = self.itemAt(event.scenePos())
    if item and item.inDraggingArea(event.scenePos()):
            QGraphicsScene.mousePressEvent(self,event)