Only move QGraphicsItem when mouse in specific region

雨燕双飞 提交于 2020-02-02 11:11:46

问题


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)


来源:https://stackoverflow.com/questions/13273785/only-move-qgraphicsitem-when-mouse-in-specific-region

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!