Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt Swipe Gesture not working
QtWS25 Last Chance

Qt Swipe Gesture not working

Scheduled Pinned Locked Moved Unsolved General and Desktop
swipegesturegesturesqgestureevent
14 Posts 5 Posters 12.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jari
    wrote on 4 Dec 2015, 09:55 last edited by
    #1

    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!

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      QTUserForAndroid
      wrote on 8 Jan 2016, 17:39 last edited by
      #2

      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

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        QTUserForAndroid
        wrote on 8 Jan 2016, 22:47 last edited by
        #3

        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

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 8 Jan 2016, 22:59 last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            QTUserForAndroid
            wrote on 9 Jan 2016, 08:58 last edited by
            #5

            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

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 9 Jan 2016, 22:28 last edited by
              #6

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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SeeLook
                wrote on 9 Jan 2016, 23:42 last edited by
                #7

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

                1 Reply Last reply
                0
                • Q Offline
                  Q Offline
                  QTUserForAndroid
                  wrote on 13 Jan 2016, 20:34 last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SeeLook
                    wrote on 13 Jan 2016, 20:56 last edited by SeeLook
                    #9

                    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.
                    1 Reply Last reply
                    0
                    • Q Offline
                      Q Offline
                      QTUserForAndroid
                      wrote on 13 Jan 2016, 21:06 last edited by
                      #10

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

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        SeeLook
                        wrote on 13 Jan 2016, 21:13 last edited by
                        #11

                        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.

                        1 Reply Last reply
                        0
                        • Q Offline
                          Q Offline
                          QTUserForAndroid
                          wrote on 14 Jan 2016, 19:43 last edited by
                          #12

                          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!!

                          1 Reply Last reply
                          1
                          • Q Offline
                            Q Offline
                            QTUserForAndroid
                            wrote on 1 Mar 2016, 19:55 last edited by
                            #13

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

                            1 Reply Last reply
                            0
                            • C Offline
                              C Offline
                              Carmoneer
                              wrote on 1 Mar 2016, 20:06 last edited by
                              #14

                              @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

                              1 Reply Last reply
                              0

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved