当QGraphicsScene鼠标松开事件响应完成后自动调用QGraphicsItem鼠标松开事件C++
时间: 2023-07-11 16:04:08 浏览: 47
在Qt中,当QGraphicsScene接收到鼠标事件后,会将其传递给QGraphicsItem。如果你想在鼠标松开事件完成后自动调用QGraphicsItem的鼠标松开事件,你可以在QGraphicsItem的鼠标松开事件中调用父类的鼠标松开事件。具体来说,你可以在QGraphicsItem的mouseReleaseEvent()函数中加入如下代码:
```
void MyItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
// 处理自身的鼠标松开事件
// ...
// 调用父类的鼠标松开事件
QGraphicsItem::mouseReleaseEvent(event);
}
```
这样,当QGraphicsScene完成鼠标松开事件的响应后,会自动调用当前QGraphicsItem的鼠标松开事件,并且该事件会传递给父类进行处理。
相关问题
当QGraphicsScene鼠标松开事件响应后调用QGraphicsItem鼠标松开事件C++
在 `QGraphicsScene` 的 `mouseReleaseEvent` 中,可以通过 `items()` 方法获取鼠标松开时所在的所有 `QGraphicsItem`,然后再分别调用它们的 `mouseReleaseEvent` 方法。
以下是一个简单的示例代码:
```cpp
void MyScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
QGraphicsScene::mouseReleaseEvent(event);
// 获取鼠标松开时所在的所有QGraphicsItem
QList<QGraphicsItem*> items = this->items(event->scenePos());
// 分别调用它们的mouseReleaseEvent方法
for (QGraphicsItem *item : items) {
item->mouseReleaseEvent(event);
}
}
```
在上面的代码中,我们首先调用了 `QGraphicsScene` 的 `mouseReleaseEvent` 方法,以保证场景能够正常响应鼠标松开事件。然后,我们通过 `this->items(event->scenePos())` 获取了鼠标松开时所在的所有 `QGraphicsItem`,并使用 `for` 循环遍历这些 `QGraphicsItem`,分别调用它们的 `mouseReleaseEvent` 方法。
先执行QGraphicsItem鼠标松开事件接着执行QGraphicsView鼠标松开事件 C++
在Qt中,QGraphicsView继承自QWidget,而QGraphicsItem继承自QGraphicsObject。因此,当在QGraphicsView中鼠标松开时,会首先调用QGraphicsView的mouseReleaseEvent(),然后再调用QGraphicsScene的mouseReleaseEvent(),最后才调用QGraphicsItem的mouseReleaseEvent()。
如果你想先执行QGraphicsItem的鼠标松开事件,可以使用eventFilter()函数来拦截QGraphicsView的鼠标松开事件,并在其中调用QGraphicsItem的mouseReleaseEvent()。以下是一个示例:
```cpp
bool MyGraphicsItem::eventFilter(QObject* obj, QEvent* event)
{
if(event->type() == QEvent::MouseButtonRelease)
{
QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
if(mouseEvent->button() == Qt::LeftButton)
{
mouseReleaseEvent(mouseEvent);
return true;
}
}
return QObject::eventFilter(obj, event);
}
void MyGraphicsView::mouseReleaseEvent(QMouseEvent* event)
{
QGraphicsView::mouseReleaseEvent(event);
}
void MyGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
{
QGraphicsScene::mouseReleaseEvent(event);
}
void MyGraphicsItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
{
// 处理鼠标松开事件
}
```
在这个示例中,我们重写了QGraphicsItem的eventFilter()函数,在其中拦截QGraphicsView的鼠标松开事件,并调用QGraphicsItem的mouseReleaseEvent()函数处理事件。在QGraphicsView和QGraphicsScene中,我们只需要调用父类的mouseReleaseEvent()函数即可。
相关推荐
![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)
![](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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)