QTouchEvent¶
The
QTouchEventclass contains parameters that describe a touch event. More…

New in version 4.6.
Synopsis¶
Functions¶
def
device()def
setDevice(adevice)def
setTarget(atarget)def
setTouchPointStates(aTouchPointStates)def
setTouchPoints(atouchPoints)def
setWindow(awindow)def
target()def
touchPointStates()def
touchPoints()def
window()
Detailed Description¶
Enabling Touch Events¶
Touch events occur when pressing, releasing, or moving one or more touch points on a touch device (such as a touch-screen or track-pad). To receive touch events, widgets have to have the
WA_AcceptTouchEventsattribute set and graphics items need to have theacceptTouchEventsattribute set to true.When using
QAbstractScrollAreabased widgets, you should enable theWA_AcceptTouchEventsattribute on the scroll area’sviewport.Similarly to
QMouseEvent, Qt automatically grabs each touch point on the first press inside a widget, and the widget will receive all updates for the touch point until it is released. Note that it is possible for a widget to receive events for numerous touch points, and that multiple widgets may be receiving touch events at the same time.
Event Handling¶
All touch events are of type
TouchBegin,TouchUpdate,TouchEndorTouchCancel. Reimplementevent()orviewportEvent()for widgets andsceneEvent()for items in a graphics view to receive touch events.Unlike widgets, QWindows receive touch events always, there is no need to opt in. When working directly with a
QWindow, it is enough to reimplementtouchEvent().The
TouchUpdateandTouchEndevents are sent to the widget or item that accepted theTouchBeginevent. If theTouchBeginevent is not accepted and not filtered by an event filter, then no further touch events are sent until the nextTouchBegin.Some systems may send an event of type
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 aTouchCancelevent so they can update their state accordingly.The
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 aTouchCancelevent. Information about each touch point can be retrieved using theTouchPointclass. TheTouchPointStateenum describes the different states that a touch point may have.Note
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 oftouchPoints().count()==2 is guaranteed to imply that the number of fingers touching the touchscreen or touchpad is exactly two.
Event Delivery and Propagation¶
By default,
QGuiApplicationtranslates the first touch point in aQTouchEventinto aQMouseEvent. This makes it possible to enable touch events on existing widgets that do not normally handleQTouchEvent. See below for information on some special considerations needed when doing this.
TouchBeginis the first touch event sent to a widget. TheTouchBeginevent contains a special accept flag that indicates whether the receiver wants the event. By default, the event is accepted. You should callignore()if the touch event is not handled by your widget. TheTouchBeginevent is propagated up the parent widget chain until a widget accepts it withaccept(), or an event filter consumes it. For QGraphicsItems, theTouchBeginevent is propagated to items under the mouse (similar to mouse event propagation for QGraphicsItems).
Touch Point Grouping¶
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
TouchBeginevents to the same widget, which could theoretically happen during propagation if, for example, the user touched 2 separate widgets in aQGroupBoxand both widgets ignored theTouchBeginevent.To avoid this, Qt will group new touch points together using the following rules:
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
QTouchEventto 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.
Mouse Events and Touch Event Synthesizing¶
QTouchEventdelivery is independent from that ofQMouseEvent. The application flagsAA_SynthesizeTouchForUnhandledMouseEventsandAA_SynthesizeMouseForUnhandledTouchEventscan be used to enable or disable automatic synthesizing of touch events to mouse events and mouse events to touch events.
Caveats¶
As mentioned above, enabling touch events means multiple widgets can be receiving touch events simultaneously. Combined with the default
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 aQSliderwith one finger and pressing aQPushButtonwith another. The signals emitted by these widgets will be interleaved.Recursion into the event loop using one of the exec() methods (e.g.,
exec()orexec()) in aQTouchEventevent 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
mouse grabor anactive pop-up 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.See also
TouchPointTouchPointStateWA_AcceptTouchEventsacceptTouchEvents()
- class PySide2.QtGui.QTouchEvent(eventType[, device=None[, modifiers=Qt.NoModifier[, touchPointStates=Qt.TouchPointStates()[, touchPoints=list()]]]])¶
- param touchPointStates:
TouchPointStates- param modifiers:
KeyboardModifiers- param eventType:
- param device:
- param touchPoints:
Constructs a
QTouchEventwith the giveneventType,device, andtouchPoints. ThetouchPointStatesandmodifiersare the current touch point states and keyboard modifiers at the time of the event.
- PySide2.QtGui.QTouchEvent._target¶
- PySide2.QtGui.QTouchEvent.device()¶
- Return type:
Returns the touch device from which this touch event originates.
- PySide2.QtGui.QTouchEvent.setDevice(adevice)¶
- Parameters:
adevice –
PySide2.QtGui.QTouchDevice
See also
- PySide2.QtGui.QTouchEvent.setTarget(atarget)¶
- Parameters:
atarget –
PySide2.QtCore.QObject
See also
- PySide2.QtGui.QTouchEvent.setTouchPointStates(aTouchPointStates)¶
- Parameters:
aTouchPointStates –
TouchPointStates
See also
- PySide2.QtGui.QTouchEvent.setTouchPoints(atouchPoints)¶
- Parameters:
atouchPoints –
See also
- PySide2.QtGui.QTouchEvent.setWindow(awindow)¶
- Parameters:
awindow –
PySide2.QtGui.QWindow
See also
- PySide2.QtGui.QTouchEvent.target()¶
- Return type:
Returns the target object within the window on which the event occurred. This is typically a
QWidgetor aQQuickItem. May be 0 when no specific target is available.
- PySide2.QtGui.QTouchEvent.touchPointStates()¶
- Return type:
TouchPointStates
Returns a bitwise OR of all the touch point states for this event.
- PySide2.QtGui.QTouchEvent.touchPoints()¶
- Return type:
Returns the list of touch points contained in the touch event.
- PySide2.QtGui.QTouchEvent.window()¶
- Return type:
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.
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.