Swipe gesture for forms transition



  • Hello,
    I'm trying to implement a transition between forms with swipe gesture.
    I followed some instruction found on the web but it doesn't work as expected.

    This is my code:
    File hpp:

    #ifndef PAGEINTRO_HPP
    #define PAGEINTRO_HPP
    
    #include <QDialog>
    #include <QSwipeGesture>
     #include <QtGui>
    
    namespace Ui {
    class PageIntro;
    }
    
    class PageIntro : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit PageIntro(QWidget *parent = 0);
        ~PageIntro();
        bool gestureEvent(QGestureEvent *event);
        void swipeTriggered(QSwipeGesture*);
    
    private slots:
        void on_pushButton_clicked();
    
    protected:
         bool event(QEvent *event);
    
    private:
        Ui::PageIntro *ui;
    };
    
    #endif // PAGEINTRO_HPP
    
    

    File cpp:

    #include "PageIntro.hpp"
    #include "ui_PageIntro.h"
    
    PageIntro::PageIntro(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::PageIntro)
    {
        setAttribute(Qt::WA_AcceptTouchEvents);
        setAttribute(Qt::WA_TouchPadAcceptSingleTouchEvents);
    
        grabGesture(Qt::SwipeGesture);
        ui->setupUi(this);
    }
    
    PageIntro::~PageIntro()
    {
        delete ui;
    }
    
    void PageIntro::on_pushButton_clicked()
    {
        this->close();
    }
    
    bool PageIntro::event(QEvent *event)
    {
        if (event->type() == QEvent::Gesture)
            return gestureEvent(static_cast<QGestureEvent*>(event));
        return QWidget::event(event);
    }
    
    bool PageIntro::gestureEvent(QGestureEvent *event)
    {
        if (QGesture *swipe = event->gesture(Qt::SwipeGesture))
            swipeTriggered(static_cast<QSwipeGesture *>(swipe));
        return true;
    }
    
    void PageIntro::swipeTriggered(QSwipeGesture *gesture)
    {
        if (gesture->state() == Qt::GestureFinished) {
            if (gesture->horizontalDirection() == QSwipeGesture::Left
                || gesture->verticalDirection() == QSwipeGesture::Up)
                this->close();
            else
                this->close();
            update();
        }
    }
    
    

    Very simple but...
    The issue is that the event seems recognized, or at least some events are recognized but not the gesture I' searching.
    In debug the event is raised but the type() is not showed.. don't know which is but the IF is not verified, so is not my swipe.

    It's possible to activate and handle a gesture for the whole form? ..or I should activate it under a specific widget?

    Bye
    Andrea


Log in to reply
 

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