Only move QGraphicsItem when mouse in specific reg

2019-05-21 04:54发布

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):

enter image description here

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条回答
萌系小妹纸
2楼-- · 2019-05-21 05:35

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)
查看更多
登录 后发表回答