1. 问题描述
QGraphicsItem 类可以响应鼠标按下事件,鼠标释放事件与鼠标移动事件的响应只到达了 GraphicsView。
2. 问题分析
默认QGraphicsItem 类只响应鼠标按下事件,
具体可参见[virtual protected] void QGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *event)的说明:
If you do reimplement this function, event will by default be accepted (see QEvent::accept()), and this item is then the mouse grabber. This allows the item to receive future move, release and doubleclick events. If you call QEvent::ignore() on event, this item will lose the mouse grab, and event will propagate to any topmost item beneath. No further mouse events will be delivered to this item unless a new mouse press event is received.
3. 解决方案
- 在鼠标按下事件的重载函数
mousePressEvent(QGraphicsSceneMouseEvent *event)中调用QEvent::accept(),即加入语句:
event->accept();
- 在
QGraphicsItem的构造函数中加入语句(没找到依据):
setFlag(QGraphicsItem::ItemIsSelectable);
版权声明:本文为amnesiagreen原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。