在 Qt c++ 中处理鼠标事件

发布于 2024-09-14 05:01:45 字数 231 浏览 8 评论 0原文

抱歉我的初学者问题... 定义当 MousePressEvent 或 MouseReleaseEvent 发生时执行的过程的最简单方法是什么?

现在我正在定义自己的类(MyGraphicsView 类),它继承 QGraphicsView 并且我正在重新实现鼠标事件(它们是虚函数)。它工作得很好,但是有什么方法可以解决这个问题而不需要定义一个新的类吗? 我可以以某种方式将事件与老虎机连接起来吗?

感谢您的帮助。

Sorry for my beginner's question...
What is the easiest way to define procedures, which are executed when MousePressEvent or MouseReleaseEvent occurs?

For now I am defining my own class (MyGraphicsView class), which inherits QGraphicsView and I am reimplementing mouse events (which are virtual functions). It works fine but is there any way to solve this problem without a need to define a new class?
Can I connect Events with Slots somehow?

Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

勿挽旧人 2024-09-21 05:01:45

Qt Center 论坛上的此帖子很好地描述了您的选择是。简而言之:

  1. 做你正在做的事情(即子类化和重新实现)

  2. 按照线程和其中的链接中的描述使用事件过滤器。< /p>

This thread on the Qt Centre forum describes quite well what your options are. Simply put:

  1. Do what you are doing (ie subclassing and reimplementing)

  2. Work with an event filter as described in the thread and link therein.

才能让你更想念 2024-09-21 05:01:45

因为鼠标事件是受保护的虚拟函数,所以最简单的方法正是您正在做的。我不认为定义子类会导致问题,所以我说坚持你所拥有的。

如果您确实想将事件与插槽连接起来,您可以使您的 mousePressEvent() 实现为子类,例如,简单地emit mousePressSignal()。当然,您还需要在子类标头的 signals 部分中声明 mousePressSignal()

Because the mouse events are protected virtual functions, the easiest approach is exactly what you are doing. I don't see any reason why defining a subclass would cause a problem, so I say stick with what you have.

If you really want to connect the events with slots, you can make your subclass implementation of mousePressEvent(), for example, simply emit mousePressSignal(). Of course, you would also need to declare mousePressSignal() in the signals section of the subclass header.

硬不硬你别怂 2024-09-21 05:01:45

我认为重写 QGraphicsView::mousePressEvent 和 QGraphisView::mouseReleaseEvent 没有问题。整个QGraphicsView/QGraphicsScene/QGraphicsItem事件处理概念是围绕虚拟事件处理函数构建的。

此外,Qt 文档还建议“您可以提供自己的自定义场景通过创建 QGraphicsView 的子类并重新实现鼠标和按键事件处理程序来实现交互。”

I can see no problem with overriding QGraphicsView::mousePressEvent and QGraphisView::mouseReleaseEvent. The whole QGraphicsView/QGraphicsScene/QGraphicsItem event-handling concept is built around virtual event-handling functions.

Additionally, also the Qt documentation suggests that "you can provide your own custom scene interaction, by creating a subclass of QGraphicsView, and reimplementing the mouse and key event handlers."

¢好甜 2024-09-21 05:01:45

从你的问题来看,

有什么办法吗
解决这个问题,而无需
定义一个新类?

答案是你不能

您必须从要处理事件的类继承。

我可以将事件与老虎机连接起来吗
不知何故?

您无法将事件连接到槽,但只有信号可以连接到槽。

你现在做的事就是要做的事。

From your question,

is there any way to
solve this problem without having to
define a new class?

The answer is No. You can't.

You have to have inherit from the class that you want to handle the events.

Can I connect Events with Slots
somehow?

No. You cannot connect events to slots but only signals can be connected to slots.

The way you are now doing is the way to do.

眼眸里的快感 2024-09-21 05:01:45

您可以使用事件过滤器:请参阅 http://doc.qt.nokia.com /4.6/qobject.html#eventFilter
或者,特别是对于图形项目,http://doc.qt.nokia。 com/4.6/qgraphicsitem.html#sceneEventFilter

请注意,对于后者,某些事件会发生变化:例如,QMouseEvent 变为 QGraphicsSceneMouseEvent,因此请确保过滤正确的类型。

哪个更容易,取决于具体情况。无论如何,如果您有一个子类,则重新实现虚拟方法通常比事件过滤器方法更直接。但是,如果您想跟踪来自多个不同类的多个小部件,并且需要对某个事件进行特殊处理,那么仅出于此目的进行子类化是乏味的,并且不是一个好的设计(并且使得例如为这些小部件使用设计器变得更加困难)。

You can use event filters: See http://doc.qt.nokia.com/4.6/qobject.html#eventFilter
Or, for graphics items in particular, http://doc.qt.nokia.com/4.6/qgraphicsitem.html#sceneEventFilter

Note that for the latter, some events change: QMouseEvent becomes QGraphicsSceneMouseEvent for example, so make sure to filter for the right type.

What is easier, depends on the situation. If you have a subclass anyway, reimplementing the virtual method is often more straight-forward than the event filter approach. But if you want to track several widgets from several different classes and need special handling for a certain event, subclassing just for this purpose is tedious and not a good design (and makes e.g. using designer for those widgets harder).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文