Gestures in Widgets and Graphics View▲
Qt includes a framework for gesture programming that has the ability to form gestures from a series of events, independently of the input methods used. A gesture could be a particular movement of a mouse, a touch screen action, or a series of events from some other source. The nature of the input, the interpretation of the gesture and the action taken are the choice of the developer.
Overview▲
https:// is the central class in Qt's gesture framework, providing a container for information about gestures performed by the user. https:// exposes properties that give general information that is common to all gestures, and these can be extended to provide additional gesture-specific information. Common panning, pinching and swiping gestures are represented by specialized classes: https://, https:// and https://.
Developers can also implement new gestures by subclassing and extending the https:// class. Adding support for a new gesture involves implementing code to recognize the gesture from input events. This is described in the https:// section.
Using Standard Gestures with Widgets▲
Gestures can be enabled for instances of https:// and https:// subclasses. An object that accepts gesture input is referred to throughout the documentation as a target object.
To enable a gesture for a target object, call its https://() or https://() function with an argument describing the required gesture type. The standard types are defined by the https:// enum and include many commonly used gestures.
for (Qt::GestureType gesture : gestures)
grabGesture(gesture);In the above code, the gestures are set up in the constructor of the target object itself.
Handling Events▲
When the user performs a gesture, https:// events will be delivered to the target object, and these can be handled by reimplementing the https://() handler function for widgets or https://() for graphics objects.
As one target object can subscribe to more than one gesture type, the https:// can contain more than one https://, indicating several possible gestures are active at the same time. It is then up to the widget to determine how to handle those multiple gestures and choose if some should be canceled in favor of others.
Each https:// contained within a https:// object can be accepted() or ignored() individually, or all together. Additionally, you can query the individual https:// data objects (the state) using several getters.
Standard Procedure for Event Handling▲
A https:// is by default accepted when it arrives at your widget. However, it is good practice to always explicitly accept or reject a gesture. The general rule is that, if you accept a gesture, you are using it. If you are ignoring it you are not interested in it. Ignoring a gesture may mean it gets offered to another target object, or it will get canceled.
Each https:// has several states it goes through; there is a well defined way to change the state, typically the user input is the cause of state changes (by starting and stopping interaction, for instance) but the widget can also cause state changes.
The first time a particular https:// is delivered to a widget or graphics item, it will be in the https:// state. The way you handle the gesture at this point influences whether you can interact with it later.
-
Accepting the gesture means the widget acts on the gesture and there will follow gestures with the Qt::GestureUpdatedstate.
-
Ignoring the gesture will mean the gesture will never be offered to you again. It will be offered to a parent widget or item as well.
-
Calling setGestureCancelPolicy() on the gesture when it is in its starting state, and is also accepted can cause other gestures to be canceled.
Using https:// to cancel a gesture will cause all gestures, in any state, to be canceled unless they are explicitly accepted. This means that active gestures on children will get canceled. It also means that gestures delivered in the same https:// will get canceled if the widget ignores them. This can be a useful way to filter out all gestures except the one you are interested in.
Example Event Handling▲
For convenience, the https:// reimplements the general https://() handler function and delegates gesture events to a specialized gestureEvent() function:
bool ImageWidget::event(QEvent *event)
{
if (event->type() == QEvent::Gesture)
return gestureEvent(static_cast<QGestureEvent*>(event));
return QWidget::event(event);
}The gesture events delivered to the target object can be examined individually and dealt with appropriately:
bool ImageWidget::gestureEvent(QGestureEvent *event)
{
qCDebug(lcExample) << "gestureEvent():" << event;
if (QGesture *swipe = event->gesture(Qt::SwipeGesture))
swipeTriggered(static_cast<QSwipeGesture *>(swipe));
else if (QGesture *pan = event->gesture(Qt::PanGesture))
panTriggered(static_cast<QPanGesture *>(pan));
if (QGesture *pinch = event->gesture(Qt::PinchGesture))
pinchTriggered(static_cast<QPinchGesture *>(pinch));
return true;
}Responding to a gesture is simply a matter of obtaining the https:// object delivered in the https:// sent to the target object and examining the information it contains.
void ImageWidget::swipeTriggered(QSwipeGesture *gesture)
{
if (gesture->state() == Qt::GestureFinished) {
if (gesture->horizontalDirection() == QSwipeGesture::Left
|| gesture->verticalDirection() == QSwipeGesture::Up) {
qCDebug(lcExample) << "swipeTriggered(): swipe to previous";
goPrevImage();
} else {
qCDebug(lcExample) << "swipeTriggered(): swipe to next";
goNextImage();
}
update();
}
}Here, we examine the direction in which the user swiped the widget and modify its contents accordingly.
Creating Your Own Gesture Recognizer▲
Adding support for a new gesture involves creating and registering a new gesture recognizer. Depending on the recognition process for the gesture, it may also involve creating a new gesture object.
To create a new recognizer, you need to subclass https:// to create a custom recognizer class. There is one virtual function that you must reimplement and two others that can be reimplemented as required.
Filtering Input Events▲
The https://() function must be reimplemented. This function handles and filters the incoming input events for the target objects and determines whether or not they correspond to the gesture the recognizer is looking for.
Although the logic for gesture recognition is implemented in this function, possibly using a state machine based on the https:// enums, you can store persistent information about the state of the recognition process in the https:// object supplied.
Your https://() function must return a value of https:// that indicates the state of recognition for a given gesture and target object. This determines whether or not a gesture event will be delivered to a target object.
Custom Gestures▲
If you choose to represent a gesture by a custom https:// subclass, you will need to reimplement the https://() function to construct instances of your gesture class instead of standard https:// instances. Alternatively, you may want to use standard https:// instances, but add additional dynamic properties to them to express specific details of the gesture you want to handle.
Resetting Gestures▲
If you use custom gesture objects that need to be reset or otherwise specially handled when a gesture is canceled, you need to reimplement the https://() function to perform these special tasks.
Note that https:// objects are only created once for each combination of target object and gesture type, and they might be reused every time the user attempts to perform the same gesture type on the target object. As a result, it can be useful to reimplement the https://() function to clean up after each previous attempt at recognizing a gesture.
Using a New Gesture Recognizer▲
To use a gesture recognizer, construct an instance of your https:// subclass, and register it with the application with https://(). A recognizer for a given type of gesture can be removed with https://().
Further Reading▲
The https:// shows how to enable gestures for a widget in a simple image viewer application.
Gestures in Qt Quick▲
Qt Quick does not have a generic global gesture recognizer; rather, individual components can respond to touch events in their own ways. For example the PinchArea handles two-finger gestures, Flickable is for flicking content with a single finger, and MultiPointTouchArea can handle an arbitrary number of touch points and allow the application developer to write custom gesture recognition code.
▲
| ^ Qt Reference Documentation ^ |


