限制qgraphicsitem的可移动区域
有没有办法限制设置 setFlag(ItemIsMovable)
时 QGraphicsItem
(如 QRect
)可以移动的区域?
我是 pyqt 的新手,试图找到一种用鼠标移动项目的方法,并将其限制为仅垂直/水平。
Is there a way to restrict the area where a QGraphicsItem
like QRect
can be moved when setFlag(ItemIsMovable)
is set?
I'm new to pyqt and trying to find a way to move an item with the mouse, and the restrict it to only vertically/horizontally.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想保留有限的区域,可以重新实现 ItemChanged()
声明:
实现:
需要 ItemSendsGeometryChanges 标志来捕获 QGraphicsItem 位置的变化
然后我们定义场景的矩形,在本例中将为 300x300
这是为了将图形保持在一个区域内,
祝你好运
If you want to keep a limited area you can reimplement the ItemChanged()
Declare:
implementation :
ItemSendsGeometryChanges flag is needed to capture the change in position of QGraphicsItem
Then we define the rectangle of the scene, in this case will be 300x300
This is to keep the graph within an area,
good luck
在 QGraphicScene 中重新实现 mouseMoveEvent(self,event)
像下面这样:
希望有帮助
re implement the mouseMoveEvent(self,event) in the QGraphicScene
like the following :
hope it helps
您可能需要重新实现
QGraphicsItem
的itemChange()
函数。伪代码:
重新定位该项目将导致再次调用
itemChange
,但这没关系,因为该项目将被正确定位并且不会再次移动,因此您不会陷入无限循环。You would probably need to re-implement the
QGraphicsItem
'sitemChange()
function.Pseudocode:
Repositioning the item will cause
itemChange
to get called again, but that's ok because the item will be positioned correctly and won't be moved again, so you'll not be stuck in an endless loop.