I solved using event filter on widget on which I want to catch TapAndHoldGesture.
I installed event filter in this way: (buttonWidget is the widget that fires TapAndHoldGesture)
buttonWidget->grabGesture(Qt::TapAndHoldGesture);
buttonWidget->installEventFilter(this);
buttonWidget->grabGesture(Qt::TapAndHoldGesture);
buttonWidget->installEventFilter(this);
To copy to clipboard, switch view to plain text mode
And then the handling code:
{
if (event
->type
() == QEvent::Gesture) { return gestureEvent(obj, static_cast<QGestureEvent*>(event));
}
return QWidget::eventFilter(obj, event
);
}
bool RecipeList
::gestureEvent(QObject *obj, QGestureEvent
*event
) {
if (QGesture *swipe = event->gesture(Qt::SwipeGesture))
swipeTriggered(obj, static_cast<QSwipeGesture*>(swipe));
else if (QGesture *tapAndHold = event->gesture(Qt::TapAndHoldGesture))
tapAndHoldTriggered(obj, static_cast<QTapAndHoldGesture*>(tapAndHold));
return true;
}
void RecipeList
::tapAndHoldTriggered(QObject *obj, QTapAndHoldGesture
* tapAndHold
) {
if (tapAndHold->state() == Qt::GestureFinished) {
QLOG_DEBUG() << obj->objectName();
}
}
bool RecipeList::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::Gesture) {
return gestureEvent(obj, static_cast<QGestureEvent*>(event));
}
return QWidget::eventFilter(obj, event);
}
bool RecipeList::gestureEvent(QObject *obj, QGestureEvent *event)
{
if (QGesture *swipe = event->gesture(Qt::SwipeGesture))
swipeTriggered(obj, static_cast<QSwipeGesture*>(swipe));
else if (QGesture *tapAndHold = event->gesture(Qt::TapAndHoldGesture))
tapAndHoldTriggered(obj, static_cast<QTapAndHoldGesture*>(tapAndHold));
return true;
}
void RecipeList::tapAndHoldTriggered(QObject *obj, QTapAndHoldGesture* tapAndHold)
{
if (tapAndHold->state() == Qt::GestureFinished) {
QLOG_DEBUG() << obj->objectName();
}
}
To copy to clipboard, switch view to plain text mode
Giovanni
Bookmarks