为什么我的QGestureRecognizer没有收到触摸事件? [英] Why does my QGestureRecognizer not receive Touch Events?

查看:351
本文介绍了为什么我的QGestureRecognizer没有收到触摸事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文:我试图创造一个推子般的小部件,可以有多个实例在同一视图,每个都可以同时被不同的手指来控制

我想使用Qt的手势识别系统,但我还需要超出标准的的Qt :: PanGesture 的一些功能。为此,我既子类 QGesture QGestureRecognizer 。在 FooGestureRecognizer ::识别(...),我目前都拦截 QMouseEvent QTouchEvent S(暂且,至少)。

I want to use Qt's gesture recognition system, but I also need some functionality above and beyond the standard Qt::PanGesture. To this end, I've subclassed both QGesture and QGestureRecognizer. In FooGestureRecognizer::recognize(...), I'm currently intercepting both QMouseEvents and QTouchEvents (for the time being, at least).

在Windows上,我只收到 QMouseEvent 秒 - 我处理他们,一切都按预期(虽然很明显我没有要处理的多点触控问题,当我输入从物理小鼠)。我收到(按顺序)的事件:

On Windows I only receive QMouseEvents - I handle them and everything works as expected (though obviously I don't have to deal with the multitouch problem when my input is from a physical mouse). The events I receive (in order):


  • QEvent的:: MouseButton preSS

  • 的字符串 QEvent的::的MouseMove 取值

  • QEvent的:: MouseButtonRelease

  • QEvent::MouseButtonPress
  • A string of QEvent::MouseMoves
  • QEvent::MouseButtonRelease

在Android上,我收到 QMouseEvent 的一个奇怪的混合和 QTouchEvent S(按顺序):

On Android, I receive a strange mix of QMouseEvents and QTouchEvents (in order):


  • QEvent的:: TouchBegin

  • QEvent的:: MouseButton preSS

  • QEvent的::的MouseMove (在位置上没有实际变化)

  • 另一个 QEvent的:: MouseButton preSS (不知道为什么我需要另一个)

  • 我实际的 QEvent的::的MouseMove s字符串,如预期

  • QEvent的:: MouseButtonRelease

  • QEvent::TouchBegin
  • QEvent::MouseButtonPress
  • QEvent::MouseMove (with no actual change in position)
  • Another QEvent::MouseButtonPress (not sure why I needed another one)
  • My actual string of QEvent::MouseMoves, as expected
  • QEvent::MouseButtonRelease

全局属性 的Qt :: AA_SynthesizeMouseForUnhandledTouchEvents 真正默认情况下。关闭它改变了我接收到的事件:

The global attribute Qt::AA_SynthesizeMouseForUnhandledTouchEvents is true by default. Turning it off changes the events I receive to:


  • QEvent的:: TouchBegin

  • QEvent::TouchBegin

......仅此而已。

...nothing else.

下面是一个precursor问题然后为我可以在里面 QGestureRecognizer什么::承认()来告诉Qt的我正在处理 QEvent的:: TouchBegin ,而且它不需要合成 QEvent的:: MouseButton preSS 给我吗? 事件 - 方式>接受()似乎并没有任何区别。

Here's a precursor question then: What can I do inside QGestureRecognizer::recognize() to tell Qt that I'm handling the QEvent::TouchBegin, and that it doesn't need to synthesize a QEvent::MouseButtonPress for me? event->accept() doesn't appear to make any difference.

实际问题:如果(因为它出现)Qt是合成的MouseEvent 从s 的TouchEvent 取值,为什么我看到我看到的 QEvent的::的MouseMove QEvent的:: MouseButtonRelease 而不是 QEvent的:: TouchUpdate QEvent的:: TouchRelease

The actual question: If (as it appears) Qt is synthesizing MouseEvents from TouchEvents, why do I see I see QEvent::MouseMove and QEvent::MouseButtonRelease but not QEvent::TouchUpdate or QEvent::TouchRelease?

code是可用的,但在简洁的利益,我不包括在这里了。如有需要,请询问。

Code is available, but in the interests of conciseness I've not included it here. Please ask if needed.

推荐答案

从的 QTouchEvent 文档

在QEvent的:: TouchUpdate和QEvent的:: TouchEnd事件被发送到接受的QEvent :: TouchBegin事件的小部件或项目。如果QEvent的:: TouchBegin事件不被接受,而不是由一个事件的过滤器过滤,然后没有进一步的触摸事件发送,直到下一个QEvent的:: TouchBegin

The QEvent::TouchUpdate and QEvent::TouchEnd events are sent to the widget or item that accepted the QEvent::TouchBegin event. If the QEvent::TouchBegin event is not accepted and not filtered by an event filter, then no further touch events are sent until the next QEvent::TouchBegin.

这个问题的根源在于 QGestureRecognizer 不接受最初的 TouchBegin ,因此我们不接收任何触摸事件。我这周围有:

The root of this problem is that QGestureRecognizer does not accept the initial TouchBegin, and hence we don't receive any further touch events. I got around this by:


  1. 创建薄事件过滤器的QObject 归我 QGestureRecognizer ,包含以下code

  1. Creating a thin event filter QObject owned by my QGestureRecognizer, containing the following code:

布尔FooGestureRecognizer :: FooEventFilter :: eventFilter(QObject的*对象,QEvent的*事件)
{
  如果(事件 - >类型()== QEvent的:: TouchBegin)
  {
    返回true;
  }
  其他
  {
    返回的QObject :: eventFilter(实物,事件);
  }
}

安装我的事件过滤器,并调用的setAttribute(Qt的:: WA_AcceptTouchEvents)上每一个有效的* 目标自带通过 FooGestureRecognizer ::创建()

Installing my event filter AND calling setAttribute(Qt::WA_AcceptTouchEvents) on every valid* Target that comes through FooGestureRecognizer::create().

返回真正 eventFilter 告诉Qt我的推杆有兴趣收到进一步的触摸事件,而这些触摸事件如预期的手势识别交付。

Returning true from eventFilter tells Qt that my fader is interested in receiving further touch events, and these touch events are delivered as expected to the gesture recognizer.

该解决方案感觉就像一个黑客攻击,另一​​个可能无法在Qt的未来版本的必要,所以我要盯紧这个code。

This solution feels like a hack, and one that might not be necessary in future versions of Qt, so I'm going to keep an eye on this code.

注:


  • 在建设一个 QGestureRecognizer 创建()是带一个空目标(预期虚 QGesture 退还)。当心这一点,如果你在所有的目标选择安装事件过滤器。

  • 我的应用程序需要处理的桌面鼠标事件的一种方式,而在另一个多指触摸事件,所以我已经停用​​的Qt :: AA_SynthesizeMouseForUnhandledTouchEvents 。保持这使可能会导致其他方面的考虑(比如我不知道,如果你需要返回true eventFilter ,以免他们被复制为合成鼠标事件)。

  • During the construction of a QGestureRecognizer, create() is called with a null Target (expecting a dummy QGesture to be returned). Watch out for this if you're installing event filters on all Targets.
  • My application needs to handle desktop mouse events in one way, and multi-finger touch events in another, so I've disabled Qt::AA_SynthesizeMouseForUnhandledTouchEvents. Keeping this enabled may lead to other considerations (e.g. I'm not sure if you'd need to return true for all touch events in eventFilter, so as to avoid them being duplicated as synthesised mouse events).

这篇关于为什么我的QGestureRecognizer没有收到触摸事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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