Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Swipe Gesture



  • Hi:
    Well, I´m new in this forum but some of the topics that are in the site have been so helpful. Just like this one:
    Re: Qt Swipe Gesture not working
    I´ve already follow all the instruccions and it work!! But i´ve been wondering if you can do it backwards I mean, that the swipe move to left to right like if you wanted to go back to the last stacked page you saw.
    Somebody could help me with this?
    Thanks.


  • Moderators

    @HollyJ
    hi,
    I made a full single finger swipe-Stackwidget a while ago, sadly I'm not allowed to share it.

    But that doesn't stop me from giving help, when its needed :-)

    Why don't you post what you have, what is working and where you have problems? As a code example, makes it easier for everyone to help/understand.



  • I´ve this swipe menu:

    bool Data1::eventFilter(QObject *, 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>(mouseevent);
    
    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();
        if((abs(diff_xpos)>300)&&(mSec<200))
        {
           if(diff_xpos<0)
           {
               TotalPages=ui->stackedWidget_2->count();
               if(mindex<TotalPages-1)
                   mindex++;
               else
                   mindex=0;
               ui->stackedWidget_2->setCurrentIndex(mindex);
           }
           else
           {
               TotalPages=ui->stackedWidget_2->count();
               if(mindex==0)
               {
                   mindex--;
               }
               ui->stackedWidget_2->setCurrentIndex(mindex);
           }
        }
    }
    else if(event->type()==QEvent::MouseMove)
    {
        //QStatusBar().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());
    }
    
    return false;
    

    }

    And it works when I click on everywhere on the right. And displace the pages from left to right. I want something to do this but backwards (right to left)cor that you can return.
    Thank you for helping me


  • Moderators

    @HollyJ
    ah, the eventFilter approach, interesting:

    From what I see, the swipe back functionality is this part

    else
           {
               TotalPages=ui->stackedWidget_2->count();
               if(mindex==0)
               {
                   mindex--;
               }
               ui->stackedWidget_2->setCurrentIndex(mindex);
           }
    

    and you would have to change that.

    You will have to remember the previous index bevor you change inte index and fall back to that
    e.g:

    else if((event->type()==QEvent::MouseButtonRelease)&&(Start==True))
    {
        Start=False;
        xpos=mouseevent.pos().x();
        diff_xpos=xpos_start-xpos;
        mSec=timer.elapsed();
        if((abs(diff_xpos)>300)&&(mSec<200))
        {
           if(diff_xpos<0)
           {
               TotalPages=ui->stackedWidget_2->count();
               if(mindex<TotalPages-1)
                   mindex++;
               else
                   mindex=0;
               lastIndex = ui->stackedWidget_2->currentIndex();
               ui->stackedWidget_2->setCurrentIndex(mindex);
           }
           else
           {
               ui->stackedWidget_2->setCurrentIndex(lastIndex);
           }
        }
    }
    


  • Hi @J-Hilk
    Thank you so much for answer me too soon. I´ve already try all what you wrote but it still working in the same way. Any suggestions? Or what I´m doing wrong?


  • Moderators

    @HollyJ
    well, I could only guess :-)

    Post what you wrote, hand how it behaves differently from what you want it to do.



  • Hi, sorry for the late respond but if some wants to do a swipe on stacked widget with buttons this is the code that works for me:
    On right button:
    curr = this->ui->YourSwipedpage->currentIndex();
    this->ui->YouSwipepage->setCurrentIndex(curr+1);
    On Left Button:
    curr = this->ui->YourSwipePage->currentIndex();
    this->ui->YourSwipePage->setCurrentIndex(curr-1);

    Thanks to J.Hilk for all his help


Log in to reply