How to Create a QTapAndHoldGesture & Swipe Gesture in Qt?



  • I am new to this qt- forum. This is My First Post. Happy to write here :-)

    Can anyone please post a simple working example for QTapAndHold Gesture and Swipe Gesture? I got very few examples from internet and tried with that.. None was working correct..



  • Welcome to Qt Forum! Hope this helps you to grab the Tap and Hold Gesture...

    @#include "MainWindow.h"
    #include "ui_MainWindow.h"
    #include <QDebug>
    #include <QTapAndHoldGesture>
    #include <QGestureEvent>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    grabGesture(Qt::TapAndHoldGesture);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    bool MainWindow::event(QEvent *event)
    {
    if (event->type() == QEvent::Gesture)
    {
    QGestureEvent gestevent = static_cast<QGestureEvent>(event);
    if (QGesture *gest = gestevent->gesture(Qt::TapAndHoldGesture))
    {
    QTapAndHoldGesture *tapgest = static_cast<QTapAndHoldGesture *>(gestevent->gesture(Qt::TapAndHoldGesture));
    qDebug() << "Gesture position is : " << tapgest->position();
    }
    return true;
    }
    return QWidget::event(event);
    }@


  • Moderators

    what do you have so far? what have you tried?
    Did you use QWidget::grabGesture()?



  • Thanks for your replies.

    I have a another question regarding the same topic. Is it possible to grab a gesture for a Particular Component (say QPushButton).

    I have a QPushButton. For that I have to grab the Gesture for it. But the Grab Gesture grabs for the whole widget and not for the particular component.

    Please give your suggestions about it.



  • Hello Raagini,

    Is the solution provided by Rochi working for you ??? Actually this solution is not working for me. I am not able to recognize the QTapAndHold Gesture. I am using Qt 4.8 on Windows 7.

    Thanks in advance....


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.