QGraphicsView不执行鼠标松开事件C++
时间: 2023-09-15 14:15:22 浏览: 66
可能是因为QGraphicsView默认情况下会将鼠标事件交给其子控件处理,而不是自己处理。你可以尝试在重载QGraphicsView的mouseReleaseEvent函数时,调用父类的mouseReleaseEvent函数,以确保鼠标松开事件被正确处理。例如:
```cpp
void MyGraphicsView::mouseReleaseEvent(QMouseEvent *event)
{
// 处理自己的逻辑...
// 调用父类的mouseReleaseEvent函数
QGraphicsView::mouseReleaseEvent(event);
}
```
另外,如果你希望QGraphicsView自己处理鼠标事件而不是交给其子控件处理,可以调用setMouseTracking(true)函数,以确保鼠标事件被正确传递。例如:
```cpp
MyGraphicsView::MyGraphicsView(QWidget *parent)
: QGraphicsView(parent)
{
setMouseTracking(true); // 开启鼠标追踪
}
```
相关问题
QGraphicsView不执行鼠标松开事件
如果你在使用QGraphicsView时发现鼠标松开事件没有被触发,可能是因为该事件被QGraphicsScene捕获并处理了。QGraphicsScene是QGraphicsView的场景,它负责管理QGraphicsItem的绘制和交互。
要在QGraphicsView中处理鼠标松开事件,你可以重写QGraphicsView的mouseReleaseEvent函数,并在其中调用父类的mouseReleaseEvent函数。示例代码如下:
```python
class MyView(QGraphicsView):
def mouseReleaseEvent(self, event):
super().mouseReleaseEvent(event)
# 处理鼠标松开事件的代码
```
如果你想要在QGraphicsScene中处理鼠标松开事件,你可以重写QGraphicsScene的mouseReleaseEvent函数,示例代码如下:
```python
class MyScene(QGraphicsScene):
def mouseReleaseEvent(self, event):
super().mouseReleaseEvent(event)
# 处理鼠标松开事件的代码
```
需要注意的是,在QGraphicsScene中处理鼠标松开事件时,如果你想要获取鼠标松开的位置,你应该使用event.scenePos()函数来获取鼠标在场景中的位置,而不是event.pos()函数获取鼠标在视图中的位置。因为在场景中,鼠标位置可能与视图中不同,例如当场景大小大于视图大小时。
QGraphicsItem QGraphicsView共同执行鼠标事件 C++
在 Qt 中,QGraphicsItem 和 QGraphicsView 都有处理鼠标事件的成员函数。为了让它们共同处理鼠标事件,你可以使用 QGraphicsView 的 viewport() 函数来获取它的视图窗口,然后将视图窗口的鼠标事件转发给场景中的 QGraphicsItem。
以下是一个示例代码:
```cpp
void MyGraphicsView::mousePressEvent(QMouseEvent *event)
{
QGraphicsView::mousePressEvent(event);
// 将鼠标事件转发给场景中的所有项
QPointF scenePos = mapToScene(event->pos());
QList<QGraphicsItem*> items = scene()->items(scenePos);
for (QGraphicsItem* item : items) {
QGraphicsSceneMouseEvent sceneEvent(QEvent::GraphicsSceneMousePress);
sceneEvent.setScenePos(scenePos);
sceneEvent.setScreenPos(event->globalPos());
sceneEvent.setButton(event->button());
sceneEvent.setButtons(event->buttons());
sceneEvent.setModifiers(event->modifiers());
item->sceneEvent(&sceneEvent);
}
}
```
在这个示例代码中,我们重载了 QGraphicsView 的 mousePressEvent() 函数,并将它的事件转发给场景中的所有项。我们首先调用 QGraphicsView 的 mousePressEvent() 函数,以便处理视图中的其他逻辑。然后,我们使用 mapToScene() 函数将视图坐标转换为场景坐标,并获取该位置上的所有项。接着,我们创建了一个 QGraphicsSceneMouseEvent 对象,设置了它的参数,并将它发送给每个场景项的 sceneEvent() 函数。
请注意,这只是一个示例代码,你可能需要根据你的应用程序的需求进行调整。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)