2012-11-07 70 views
2

我试图在python中使用PySide创建类似于terragens节点网络视图的东西。
我使用此代码子类QGraphicsRectItem。只在特定区域中移动鼠标时移动QGraphicsItem

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

其中给出了这样的(有一些奇特的画):

enter image description here

我想implent通过拖动从一个小矩形到另一个鼠标连接节点,但这会导致移动整个节点。

所以我不想让QGraphicsRectItem在鼠标在一个小矩形内按下时移动。我将如何能够做到这一点。

(如果需要的话,我可以定义类似isInDraggingArea(x,y)

在此先感谢。

回答

2

我找到了解决方案,很抱歉浪费你的时间。

在场景中添加以下代码:

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