将事件传递/转发到Qt中的子对象 [英] Passing / forwarding events to a sub-object in Qt

查看:270
本文介绍了将事件传递/转发到Qt中的子对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个Qt应用程序,并且我想要获取鼠标事件时遇到问题。



以下是我所拥有的高级视图(每个级别还有其他一些事情需要视图和场景):

  + ---------------- 
|应用程序窗口 - QMainWindow
| + --------------
| |视图 - QGraphicsView ---抓住模式1
|的鼠标事件| + ------------
| | |场景 - QGraphicsScene
| | | + ----------
| | | |图像 - QGraphicsPixmap ---想要在这里抓住模式2的$鼠标事件
| | | |

在模式1中,我正在抓住事件 mousePressEvent em>查看级别。在模式2中,我想在图像级别抓住鼠标事件。在我在View类中重载了mousePressEvent 之前,我能够获得图像中的所有事件。现在我正在捕获 我无法在图像中获取鼠标事件。 中是私人的,我不想公开。



这里没有继承;每个项目是由它上面的对象创建的对象。



阅读这个问题 Qt - 将事件传递给多个对象?使得它似乎是要在图像级别捕获鼠标事件,并让它们返回向上;但是,我仍然需要在查看中创建

mousePressEvent ,我将会回到我的位置。



有没有办法在运行时打开/关闭事件处理,并确保它们进入较低级别?还是有一种将事件转发到子对象的方法?

解决方案

有没有办法打开/在运行期间关闭事件处理,并确保它们进入较低级别?还是有办法将事件转发到子对象?



有一些可能性:


  1. 使用 QGraphicsScene :: sendEvent (...)将事件发送到项目。因为场景()可以通过方法场景()从任何项目访问。也许这是一个解决方案。

  2. 您可以使用静态方法 QCoreApplication :: sendEvent (...)来执行相同的工作。

  3. 事件过滤器可以执行此操作,请参阅事件和事件过滤器一章

  4. 可以使用Graphicsview框架的特定安装事件系统 QGraphicsItem :: sceneEventFilter (...)和 installSceneEventFilter (...)

在运行时,可以使用方法QGraphicsItem :: setAcceptedMouseButtons (...)关闭/关闭鼠标事件。



还有很多其他的可能性...



希望它有帮助!


I'm working on a Qt application and I have a problem with getting mouse events where I want them.

Here's a high-level view of what I have (there are other things going on at each level that dictate the need for the views and scenes):

+----------------
| App Window       -- QMainWindow
| +--------------
| | View           -- QGraphicsView   --- Grabbing mouse events here for Mode 1
| | +------------
| | | Scene        -- QGraphicsScene
| | | +----------
| | | | Image      -- QGraphicsPixmap --- Want to grab mouse events here for Mode 2
| | | | 

In Mode 1, I'm grabbing the event mousePressEvent at the View level. In Mode 2, I'd like to grab the mouse events at the Image level. Before I overloaded mousePressEvent in the View class I was able to get all events in Image. Now that I'm capturing at View I can't get mouse events in Image. Image is private within Scene and I don't want to expose it.

There is no inheritance here; each item is an object created by the object above it.

Reading through this question Qt -- pass events to multiple objects? makes it seem like I want to capture mouse events at the Image level and let them go back up; however, I would still need to create mousePressEvent in View and I'd be right back where I am.

Is there a way to turn on/off event handling during run-time and make sure they get to the lower levels? Or is there a way to forward events to a sub-object?

解决方案

Is there a way to turn on/off event handling during run-time and make sure they get to the lower levels? Or is there a way to forward events to a sub-object?

There are some possibilities :

  1. Use QGraphicsScene::sendEvent(...) to send event to items. As the scene() is accessible from any items by the method scene(). Maybe it's a solution for you.
  2. You can use the static method QCoreApplication::sendEvent (...) for doing the same job.
  3. Event filter can do the job, see Events and event filters chapter.
  4. You can use specific install event system of Graphicsview framework (QGraphicsItem::sceneEventFilter(...) and installSceneEventFilter(...)

At runtime, it's possible to turn off/on mouse event with the method QGraphicsItem::setAcceptedMouseButtons(...).

There are many other possibilities...

Hope it helps !

这篇关于将事件传递/转发到Qt中的子对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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