[SOLVED] QSwipeGesture implementation on Desktop



  • Hi,

    I am trying to add features to my application (Desktop, target OS Win7) by allowing swipe gestures. This application runs on a touch screen PC.

    Basically, I sub-classed a QStackedWidget and would like to switch widgets when a swipe gesture has occurred. I read through the QGesture doc's and used the following code to implement the gesture:

    @
    StackedWidget::StackedWidget(QWidget *parent) :
    QStackedWidget(parent)
    {
    // set window attributes in constructor
    setAttribute(Qt::WA_AcceptTouchEvents, true);
    }

    /*
    // allow gestures (public function)
    */
    void StackedWidget::allowGestures(bool enabled)
    {
    // check if enabled
    if (enabled)
    grabGesture(Qt::SwipeGesture);
    else
    ungrabGesture(Qt::SwipeGesture);
    }

    /*
    // event override for gesture recognizer (protected)
    */
    bool StackedWidget::event(QEvent event)
    {
    // gesture event?
    if (event->type() == QEvent::Gesture){
    return gestureEvent(static_cast<QGestureEvent
    >(event));
    }
    return QWidget::event(event);
    }

    /*
    // pick up the gesture event type (private function)
    */
    bool StackedWidget::gestureEvent(QGestureEvent *event)
    {
    // swipe gesture
    if (QGesture *swipe = event->gesture(Qt::SwipeGesture))
    swipeTriggered(static_cast<QSwipeGesture *>(swipe));
    return true;
    }

    /*
    // do swipe gesture event (private function)
    */
    void StackedWidget::swipeTriggered(QSwipeGesture *gesture)
    {
    // check if the gesture is finished
    if (gesture->state() != Qt::GestureFinished)
    return;

    // check if the gesture was a vertical swipe up
    if (gesture->verticalDirection() == QSwipeGesture::Up){
        // get the pages count and check if its already on the last page
        if (currentIndex() <= 0)
            return;
    
        // emit signal to do gesture
        emit gestureChangePage(currentIndex() - 1);
    }
    else if (gesture->verticalDirection() == QSwipeGesture::Down){
        // check if the current index is already at its max
        if (currentIndex() >= (count() - 1))
            return;
    
        // emit signal to do gesture
        emit gestureChangePage(currentIndex() + 1);
    }
    
    // update the widget
    update();
    

    }

    @

    The signal gestureChangePage() is connected in the main ui to a slot that just changes the current index on the stacked widget.

    I have a couple questions regarding the gestures:
    A. Is there a way to emulate this on a desktop environment without a touchscreen? I cant seem to get the event override to register a "gesture" event. Should this work without a touchscreen as well?
    B. I have brought this to a touch screen and tried it on the touch screen and was unable to get it to work.
    C. Is there any other software that needs to be implemented for the Gestures to register?

    Thank you!



  • Sorry for the repost...but could anyone help with this?


  • Moderators

    [quote author="dvez43" date="1369063734"]
    A. Is there a way to emulate this on a desktop environment without a touchscreen? I cant seem to get the event override to register a "gesture" event. Should this work without a touchscreen as well?
    [/quote]

    Yes you can simulate gestures by yourself, but it needs some effort made by you. You will need to implement an (transparent) overlay over your application/widget and capture the mouse events, determine the directions of the segments, interpret the gesture and send the appropriate gesture event to the widget with QApplication::postEvent().

    There is an "article":http://doc.qt.digia.com/qq/qq18-mousegestures.html available how to interpret mouse events to gestures. You will have to do the event sending by yourself though, but this is the easiest part ;)

    [quote author="dvez43" date="1369063734"]
    B. I have brought this to a touch screen and tried it on the touch screen and was unable to get it to work.
    C. Is there any other software that needs to be implemented for the Gestures to register?
    [/quote]

    In order for widgets to receive touch gestures you need to "register":http://qt-project.org/doc/qt-4.8/qwidget.html#grabGesture them.



  • Thank you for the reply. I am still working on trying to get it working on our touch screens. I didnt realize I had to implement mouse events and post events to emulate the touch screen. It seems I have the code correct then (I had already added the grabGesture for the swipe :) )

    I will give it a shot! Thank you!


Log in to reply
 

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