仅当鼠标在特定区域时移动 QGraphicsItem [英] Only move QGraphicsItem when mouse in specific region

查看:58
本文介绍了仅当鼠标在特定区域时移动 QGraphicsItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 PySide 在 python 中创建类似于 terragens 节点网络视图的内容.
我使用此代码对 QGraphicsRectItem 进行了子类化.

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.

所以我不希望在小矩形内按下鼠标时 QGraphicsRectItem 被移动.我怎么能做到这一点.

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.

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

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

提前致谢.

推荐答案

我找到了解决方案,抱歉浪费您的时间.

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)

这篇关于仅当鼠标在特定区域时移动 QGraphicsItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆