QTouchEvent 类

The QTouchEvent 类包含触摸事件的描述参数。 更多...

头: #include <QTouchEvent>
Since: Qt 4.6
继承: QInputEvent

公共类型

class TouchPoint
enum DeviceType { TouchScreen, TouchPad }

公共函数

QTouchEvent (QEvent::Type eventType , QTouchEvent::DeviceType deviceType = TouchScreen, Qt::KeyboardModifiers modifiers = Qt::NoModifier, Qt::TouchPointStates touchPointStates = 0, const QList<QTouchEvent::TouchPoint> & touchPoints = QList<QTouchEvent::TouchPoint> ())
~QTouchEvent ()
QTouchEvent::DeviceType deviceType () const
Qt::TouchPointStates touchPointStates () const
const QList<QTouchEvent::TouchPoint> & touchPoints () const
QWidget * widget () const

额外继承成员

详细描述

The QTouchEvent 类包含触摸事件的描述参数。

启用触摸事件

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

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

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

事件处理

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

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

The touchPoints () function returns a list of all touch points contained in the event. Information about each touch point can be retrieved using the QTouchEvent::TouchPoint 类。 Qt::TouchPointState 枚举描述触摸点可能拥有的不同状态。

事件的交付和传播

默认情况下, QWidget::event () translates the first non-primary touch point in a QTouchEvent QMouseEvent 。这使启用触摸事件成为可能,当现有 Widget 通常不处理 QTouchEvent 。见下文,了解某些需要特殊注意事项的有关信息,当这样做时。

QEvent::TouchBegin 是发送给 Widget 的第一触摸事件。 QEvent::TouchBegin 事件包含指示接收者是否想要事件的特殊接受标志。默认情况下,接受事件。应该调用 ignore () 若 Widget 不处理触摸事件。 QEvent::TouchBegin 事件会沿父级 Widget 链向上传播,直到小部件接受它采用 accept (),或事件过滤器消耗掉它。对于 QGraphicsItems, QEvent::TouchBegin 事件被传播给鼠标下项 (类似于 QGraphicsItems 的鼠标事件传播)。

触摸点分组

如上所述,它是可能的几个 Widget 可以同时接收 QTouchEvents。不管怎样,Qt 确保从不重复发送 QEvent::TouchBegin 事件给同一 Widget,理论上传播期间可能发生,例如,若用户触摸 2 单独 Widget 在 QGroupBox 与两 Widget 忽略 QEvent::TouchBegin 事件。

为避免这,Qt 使用下列规则将新触摸点分组到一起:

  • 当检测到第一触摸点时,目的地 Widget 首先由屏幕位置确定,其次由传播规则确定。
  • 当检测到其它触摸点时,Qt 首先查看新触摸点下 Widget 的任何祖先 (或后代) 中是否有任何活动触摸点。若有,分组新触摸点与第一触摸点,并发送新触摸点按单 QTouchEvent 到处理第一触摸点的 Widget (新触摸点下的小部件不接收事件)。

这使同级 Widget 能够独立处理触摸事件,同时确保 QTouchEvents 序列始终正确成为可能。

Mouse Events and the Primary Touch Point

QTouchEvent 的交付独立于 QMouseEvent . On some windowing systems, mouse events are also sent for the primary touch point . This means it is possible for your widget to receive both QTouchEvent and QMouseEvent for the same user interaction point. You can use the QTouchEvent::TouchPoint::isPrimary () function to identify the primary touch point.

Note that on some systems, it is possible to receive touch events without a primary touch point. All this means is that there will be no mouse event generated for the touch points in the QTouchEvent .

告诫

  • 如上所述,启用触摸事件意味着多个 Widget 可以同时接收触摸事件。组合默认 QWidget::event () 处理 QTouchEvents,这为设计触摸用户界面赋予了很大灵活性。要意识到其中所隐含的。例如,用户可能正移动 QSlider 用一根手指并按下 QPushButton 用另一根手指。由这些 Widget 发射的信号将交错。
  • 递归到事件循环使用某一 exec() 方法 (如 QDialog::exec () 或 QMenu::exec ()) 在 QTouchEvent 事件处理程序不支持。由于存在多个事件收件人,递归可能导致问题,包括但不限于丢失事件和意外无限递归。
  • QTouchEvents 不受影响被 鼠标抓取 活动弹出 Widget 。QTouchEvents 的行为未定义,当打开弹出窗口 (或抓取鼠标) 且存在多个活动触摸点时。

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

成员类型文档编制

enum QTouchEvent:: DeviceType

This enum represents the type of device that generated a QTouchEvent .

常量 描述
QTouchEvent::TouchScreen 0 In this type of device, the touch surface and display are integrated. This means the surface and display typically have the same size, such that there is a direct relationship between the touch points' physical positions and the coordinate reported by QTouchEvent::TouchPoint . As a result, Qt allows the user to interact directly with multiple QWidgets and QGraphicsItems at the same time.
QTouchEvent::TouchPad 1 In this type of device, the touch surface is separate from the display. There is not a direct relationship between the physical touch location and the on-screen coordinates. Instead, they are calculated relative to the current mouse position, and the user must use the touch-pad to move this reference point. Unlike touch-screens, Qt allows users to only interact with a single QWidget or QGraphicsItem at a time.

成员函数文档编制

QTouchEvent:: QTouchEvent ( QEvent::Type eventType , QTouchEvent::DeviceType deviceType = TouchScreen, Qt::KeyboardModifiers modifiers = Qt::NoModifier, Qt::TouchPointStates touchPointStates = 0, const QList < QTouchEvent::TouchPoint > & touchPoints = QList<QTouchEvent::TouchPoint> ())

构造 QTouchEvent 采用给定 eventType , deviceType ,和 touchPoints touchPointStates and modifiers 是事件发生时当前触摸点状态和键盘修饰符。

QTouchEvent:: ~QTouchEvent ()

销毁 QTouchEvent .

QTouchEvent::DeviceType QTouchEvent:: deviceType () const

Returns the touch device Type, which is of type DeviceType .

Qt::TouchPointStates QTouchEvent:: touchPointStates () const

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

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

返回触摸事件中包含的触摸点列表。

QWidget * QTouchEvent:: widget () const

返回事件在其中发生的 Widget。