Qt Swipe Gesture not working



  • Hello all together,

    I am trying to enable a swipe gesture in my widget, but unfortunately it is not working.
    I followed the documentations and examples that are provided by Qt.

    Here is my code:

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        grabGesture(Qt::SwipeGesture);
    }
    
    bool MainWindow::event(QEvent *event){
        if (event->type() == QEvent::Gesture)
            return gestureEvent(static_cast<QGestureEvent*>(event));
        return QWidget::event(event);
    }
    
    bool MainWindow::gestureEvent(QGestureEvent *event){
        if (QGesture *swipe = event->gesture(Qt::SwipeGesture))
            swipeTriggered(static_cast<QSwipeGesture *>(swipe));
        return true;
    }
    
    void MainWindow::swipeTriggered(QSwipeGesture *gesture){
        if (gesture->state() == Qt::GestureFinished){
            if (gesture->verticalDirection() == QSwipeGesture::Up)
                goButtomLabel();
            if (gesture->verticalDirection() == QSwipeGesture::Down)
                goUpperLabel();
            update();
        }
    }
    
    void MainWindow::goButtomLabel(){
        // does something here
    }
    
    void MainWindow::goUpperLabel(){
       // does something here
    }
    
    

    Is there anything wrong? Did I forget something?

    I am new to Qt and also a beginner with C++, so I would really appreciate your help.

    Thank you so much!



  • Hi Did you manage to solve your problem with a swipe action? If solved, please reply with the solution.
    I am having the same problem, all other standard gestures are working except for swipe. I am new to QT programmer, could not find solutions on the web yet,

    Thanks



  • Just noted, that if i use three fingers i can get a swipe event. Why three fingers? Is this a bug in QT 5.5.1?

    Thanks


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Depending on your OS swipe requires two or three fingers. e.g OS X: two fingers for a page, three for full screen apps.



  • Hi Thank you,

    In a Mobile Android Touch Screen application, if i want to swipe left or right (with one finger) to move between pages e.g. MainWindow and another window (with QT Widgets on it), do i need another gesture? I would think that swipe (with one finger) is the gesture to use, but it seems to be not correct? In Many Android Apps you can swipe left / right with one finger to move between pages.

    Thanks


  • Lifetime Qt Champion

    IIRC, one finger gestures might be seen as mouse events depending on the platform.



  • Hi
    Here is a few thoughts more about single finger swipe:
    https://forum.qt.io/topic/62385/one-finger-swipe-gestures/3



  • Thanks, i managed to write a application which tracks your single finger or mouse movement and display the X,Y coordinates on a statusbar.

    Now i need to write my own code to detect a swipe on this X,Y positions, Probably checking the distance and direction your finger moved within a short amount of time?? Any ideas on how i can do this? How can i measure time in uSec?

    Anyway, to detect the X,Y position of single finger / mouse i used the example from this post
    http://stackoverflow.com/questions/1935021/getting-mousemoveevents-in-qt

    Thanks for your help



  • You might give a try to use touch event itself...
    http://doc.qt.io/qt-5/qtouchevent.html
    To get it working there are a few thing more to prepare, but QTouchEvent returns angel, velocity, start position, current position and so; in short all needed data.

    but for ordinary mouse event:

    • in mousePressEvent() registry start position and run timer (i.e. QElapsedTimer::start())
    • in mouseMoveEvent() check time and compare current position with start position to react during swiping already
    • or handle museReleaseEvent() to act after finger was taken out.


  • Thank you, this will help. QTouch Event seems to be useful. Will study this first.



  • Also consider to take some minimal distance for any touch-move related events to distinguish just touch from swipe.
    Real finger is less precise thing in contact with touch screen.



  • Thank you for the help, i managed to get a StackedWidget to rotate between the three pages by swiping left and right working. May some tweaking needed to get the distance traveled (pixel) vs time to your liking.

    To help others struggling with this, here is my example (maybe not the best way of doing it but hey, it is working!!) The Puhbuttons may be removed, that was to move between the pages before i got the swipe working.

    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    #include <QDebug>
    //#include <QtWidgets>

    #include <QStatusBar>
    #include <QElapsedTimer>

    #include <QMouseEvent>

    #define True 1
    #define true 1
    #define False 0
    #define false 0

    int mIndex = 0;
    QElapsedTimer timer;

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

    //Do your startup stuff here
    qApp->installEventFilter(this); // For Mouse events
    timer.start(); // General timer, used by Mouse Swipe event
    
    mIndex = ui->stackedWidget->currentIndex();
    //ui->stackedWidget->setCurrentIndex(0);
    

    }

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

    // ******************************************************************
    bool MainWindow::eventFilter(QObject *obj, QEvent *event)
    {
    static bool Start = False;
    static int xpos_start = 0;
    int xpos;
    int diff_xpos;
    static qint64 mSec;

    int TotalPages;

    QMouseEvent mouseEvent = static_cast<QMouseEvent>(event);

    if((event->type() == QEvent::MouseButtonPress) && (Start == False))
    {
    Start = True;
    timer.restart();
    xpos_start = mouseEvent->pos().x();
    }
    else if((event->type() == QEvent::MouseButtonRelease) && (Start == True))
    {
    Start = False;
    xpos = mouseEvent->pos().x();
    diff_xpos = xpos_start-xpos;
    mSec = timer.elapsed();
    //ui->Time_Label->setText(QString::number(mSec));
    //600 pixels typical 178 ms
    if ((abs(diff_xpos) > 300) && (mSec < 200))// Should at least have moved x pixels
    {
    if (diff_xpos < 0)
    {
    //Move Right
    //ui->Swipe_Label->setText("Swipe Right");
    TotalPages = ui->stackedWidget->count();
    if(mIndex < TotalPages-1)
    mIndex++;
    else
    mIndex = 0;

               //if (mIndex == 3)
                //   mIndex = 0;
    
               ui->stackedWidget->setCurrentIndex(mIndex);
           }
           else
           {
               //Move Left
               //ui->Swipe_Label->setText("Swipe Left");
               TotalPages = ui->stackedWidget->count();
               if (mIndex == 0)
               {
                   mIndex = TotalPages-1;
               }
               else
               {
                   mIndex--;
               }
               ui->stackedWidget->setCurrentIndex(mIndex);
           }
        }
    }
    else if (event->type() == QEvent::MouseMove)
     {
         statusBar()->showMessage(QString("Mouse move (%1,%2)").arg(mouseEvent->pos().x()).arg(mouseEvent->pos().y()));
         qDebug() << QString::number(mouseEvent->pos().x());
         qDebug() << QString::number(mouseEvent->pos().y());
         //Show x and y coordinate values of mouse cursor here
         //ui->CoordinateLabel->setText("X:"+QString::number(mouseEvent->x())+"-- Y:"+QString::number(mouseEvent->y()));
     }
    

    return false;
    }
    // ******************************************************************

    void MainWindow::on_pushButton_0_clicked()
    {
    if(mIndex < ui->stackedWidget->count())
    mIndex++;

    ui->stackedWidget->setCurrentIndex(mIndex);
    

    }

    void MainWindow::on_pushButton_1_clicked()
    {
    if(mIndex < ui->stackedWidget->count())
    mIndex++;

    ui->stackedWidget->setCurrentIndex(mIndex);
    

    }

    void MainWindow::on_pushButton_2_clicked()
    {
    if(mIndex < ui->stackedWidget->count())
    mIndex++;
    else
    mIndex = 0;

    if (mIndex == 3)
        mIndex = 0;
    
    ui->stackedWidget->setCurrentIndex(mIndex);
    

    }

    And then the H file add the following
    private slots:
    bool eventFilter(QObject *obj, QEvent *event);

    Thanks again!!



  • Hi, this topic is solved. Should i not mark it somewhere as solved? How do i mark it as solved?
    Thanks



  • @QTUserForAndroid This should help to 'mark as solved' and other forum inquiries:

    https://forum.qt.io/topic/62700/hitchhiker-s-visual-guide-to-the-qt-forum


Log in to reply
 

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