为什么我的QGestureRecognizer没有收到触摸事件? [英] Why does my QGestureRecognizer not receive Touch Events?
问题描述
的上下文:我试图创造一个推子般的小部件,可以有多个实例在同一视图,每个都可以同时被不同的手指来控制的
我想使用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 QMouseEvent
s and QTouchEvent
s (for the time being, at least).
在Windows上,我只收到 QMouseEvent
秒 - 我处理他们,一切都按预期(虽然很明显我没有要处理的多点触控问题,当我输入从物理小鼠)。我收到(按顺序)的事件:
On Windows I only receive QMouseEvent
s - 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::MouseMove
s QEvent::MouseButtonRelease
在Android上,我收到 QMouseEvent
的一个奇怪的混合和 QTouchEvent
S(按顺序):
On Android, I receive a strange mix of QMouseEvent
s and QTouchEvent
s (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::MouseMove
s, 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 MouseEvent
s from TouchEvent
s, 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:
-
创建薄事件过滤器
的QObject
归我QGestureRecognizer
,包含以下code
Creating a thin event filter
QObject
owned by myQGestureRecognizer
, 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 全触摸事件code>,以免他们被复制为合成鼠标事件)。
的
- During the construction of a
QGestureRecognizer
,create()
is called with a nullTarget
(expecting a dummyQGesture
to be returned). Watch out for this if you're installing event filters on allTarget
s. - 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 toreturn true
for all touch events ineventFilter
, so as to avoid them being duplicated as synthesised mouse events).
这篇关于为什么我的QGestureRecognizer没有收到触摸事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!