QTouchEvent 类

QTouchEvent class contains parameters that describe a touch event. 更多...

头: #include <QTouchEvent>
qmake: QT += gui
Since: Qt 4.6
继承: QInputEvent

公共类型

class TouchPoint

公共函数

QTouchEvent (QEvent::Type eventType , QTouchDevice * device = Q_NULLPTR, Qt::KeyboardModifiers modifiers = Qt::NoModifier, Qt::TouchPointStates touchPointStates = Qt::TouchPointStates(), const QList<QTouchEvent::TouchPoint> & touchPoints = QList<QTouchEvent::TouchPoint> ())
~QTouchEvent ()
QTouchDevice * device () const
QObject * target () const
Qt::TouchPointStates touchPointStates () const
const QList<QTouchEvent::TouchPoint> & touchPoints () const
QWindow * window () const

额外继承成员

详细描述

QTouchEvent class contains parameters that describe a touch event.

启用触摸事件

触摸事件发生,当在触摸设备 (譬如:触摸屏或跟踪板) 中按下、释放或移动一个或多个触摸点时。要接收触摸事件,Widget 必须拥有 Qt::WA_AcceptTouchEvents 属性设置和图形项需要拥有 acceptTouchEvents 属性设置为 true。

当使用 QAbstractScrollArea 基 Widget,应启用 Qt::WA_AcceptTouchEvents 属性在卷动区域 viewport .

类似于 QMouseEvent ,Qt 自动抓取每个触摸点当在 Widget 内第一次按下时,且小部件将接收触摸点的所有更新,直到它被释放。注意,Widget 能接收很多触摸点事件,且多个小部件可能同时接收触摸事件。

事件处理

所有触摸事件类型,包括 QEvent::TouchBegin , QEvent::TouchUpdate , QEvent::TouchEnd or QEvent::TouchCancel 。重实现 QWidget::event () 或 QAbstractScrollArea::viewportEvent () 对于 Widget 和 QGraphicsItem::sceneEvent () 对于要接收触摸事件的图形视图项。

不像 Widget,QWindows 始终接收触摸事件,无需选项。当直接工作于 QWindow ,是足够的重实现 QWindow::touchEvent ().

QEvent::TouchUpdate and QEvent::TouchEnd 事件会被发送给 Widget 或项接受 QEvent::TouchBegin 事件。若 QEvent::TouchBegin 事件未接受且未被事件过滤器所过滤,则进一步触摸事件不会被发送直到下一 QEvent::TouchBegin .

某些系统可能发送事件类型 QEvent::TouchCancel . Upon receiving this event applications are requested to ignore the entire active touch sequence. For example in a composited system the compositor may decide to treat certain gestures as system-wide gestures. Whenever such a decision is made (the gesture is recognized), the clients will be notified with a QEvent::TouchCancel event so they can update their state accordingly.

touchPoints () function returns a list of all touch points contained in the event. Note that this list may be empty, for example in case of a QEvent::TouchCancel event. Information about each touch point can be retrieved using the QTouchEvent::TouchPoint 类。 Qt::TouchPointState enum describes the different states that a touch point may have.

注意: The list of touchPoints () will never be partial: A touch event will always contain a touch point for each existing physical touch contacts targetting the window or widget to which the event is sent. For instance, assuming that all touches target the same window or widget, an event with a condition of touchPoints ().count()==2 is guaranteed to imply that the number of fingers touching the touchscreen or touchpad is exactly two.

事件交付和传播

默认情况下, QGuiApplication translates the first touch point in a QTouchEvent QMouseEvent . This makes it possible to enable touch events on existing widgets that do not normally handle QTouchEvent . See below for information on some special considerations needed when doing this.

QEvent::TouchBegin 是发送给 Widget 的第一个触摸事件。 QEvent::TouchBegin event contains a special accept flag that indicates whether the receiver wants the event. By default, the event is accepted. You should call ignore () if the touch event is not handled by your widget. The QEvent::TouchBegin event is propagated up the parent widget chain until a widget accepts it with accept (), or an event filter consumes it. For QGraphicsItems, the QEvent::TouchBegin event is propagated to items under the mouse (similar to mouse event propagation for QGraphicsItems).

触摸点分组

As mentioned above, it is possible that several widgets can be receiving QTouchEvents at the same time. However, Qt makes sure to never send duplicate QEvent::TouchBegin events to the same widget, which could theoretically happen during propagation if, for example, the user touched 2 separate widgets in a QGroupBox and both widgets ignored the QEvent::TouchBegin 事件。

为避免这,Qt 将使用以下规则将新的接触点分组在一起:

  • When the first touch point is detected, the destination widget is determined firstly by the location on screen and secondly by the propagation rules.
  • When additional touch points are detected, Qt first looks to see if there are any active touch points on any ancestor or descendent of the widget under the new touch point. If there are, the new touch point is grouped with the first, and the new touch point will be sent in a single QTouchEvent to the widget that handled the first touch point. (The widget under the new touch point will not receive an event).

This makes it possible for sibling widgets to handle touch events independently while making sure that the sequence of QTouchEvents is always correct.

鼠标事件和触摸事件合成

QTouchEvent delivery is independent from that of QMouseEvent 。应用程序标志 Qt::AA_SynthesizeTouchForUnhandledMouseEvents and Qt::AA_SynthesizeMouseForUnhandledTouchEvents 可以用于启用 (或禁用) 将触摸事件自动合成为鼠标事件,和将鼠标事件自动合成为触摸事件。

告诫

  • As mentioned above, enabling touch events means multiple widgets can be receiving touch events simultaneously. Combined with the default QWidget::event () handling for QTouchEvents, this gives you great flexibility in designing touch user interfaces. Be aware of the implications. For example, it is possible that the user is moving a QSlider with one finger and pressing a QPushButton with another. The signals emitted by these widgets will be interleaved.
  • Recursion into the event loop using one of the exec() methods (e.g., QDialog::exec () 或 QMenu::exec ()) in a QTouchEvent event handler is not supported. Since there are multiple event recipients, recursion may cause problems, including but not limited to lost events and unexpected infinite recursion.
  • QTouchEvents are not affected by a 鼠标抓取 活动弹出 Widget . The behavior of QTouchEvents is undefined when opening a pop-up or grabbing the mouse while there are more than one active touch points.

另请参阅 QTouchEvent::TouchPoint , Qt::TouchPointState , Qt::WA_AcceptTouchEvents ,和 QGraphicsItem::acceptTouchEvents ().

成员函数文档编制

QTouchEvent:: QTouchEvent ( QEvent::Type eventType , QTouchDevice * device = Q_NULLPTR, Qt::KeyboardModifiers modifiers = Qt::NoModifier, Qt::TouchPointStates touchPointStates = Qt::TouchPointStates(), const QList < QTouchEvent::TouchPoint > & touchPoints = QList<QTouchEvent::TouchPoint> ())

构造 QTouchEvent 采用给定 eventType , device ,和 touchPoints touchPointStates and modifiers are the current touch point states and keyboard modifiers at the time of the event.

QTouchEvent:: ~QTouchEvent ()

销毁 QTouchEvent .

QTouchDevice *QTouchEvent:: device () const

Returns the touch device from which this touch event originates.

QObject *QTouchEvent:: target () const

Returns the target object within the window on which the event occurred. This is typically a QWidget QQuickItem 。可能为 0 当没有可用特定目标时。

Qt::TouchPointStates QTouchEvent:: touchPointStates () const

返回此事件所有触摸点状态的按位 OR。

const QList < QTouchEvent::TouchPoint > &QTouchEvent:: touchPoints () const

Returns the list of touch points contained in the touch event.

QWindow *QTouchEvent:: window () const

Returns the window on which the event occurred. Useful for doing global-local mapping on data like rawScreenPositions() which, for performance reasons, only stores the global positions in the touch event.