Pan and swipe gesture in Qt on Nokia N8 device



  • Hi

    I've been trying to implement gestures in Qt. I've understood from reading forums that the gestures does not work on Windows or Linux desktops (which is stupid according to me since it makes testing and debugging more difficult) but we have to live with that.

    But I can not get pan or swipe gestures as they are documented in "http://doc.qt.nokia.com/4.7/qpangesture.html":http://doc.qt.nokia.com/4.7/qpangesture.html to work on my Nokia N8 device either. I've done the following:

    @
    MyWidget::MyWidget(QWidget *parent) :
    : QWidget(parent)
    {
    setAttribute(Qt::WA_AcceptTouchEvents);
    grabGesture(Qt::PanGesture);
    grabGesture(Qt::SwipeGesture);
    }

    MyWidget::~MyWidget()
    {
    }

    bool MyWidget::event(QEvent event)
    {
    if (event->type() == QEvent::Gesture) {
    return gestureEvent(static_cast<QGestureEvent
    >(event));
    }
    return QWidget::event(event);
    }

    bool MyWidget::gestureEvent(QGestureEvent *event)
    {
    if (QGesture *swipe = event->gesture(Qt::SwipeGesture)) {
    swipeTriggered(static_cast<QSwipeGesture *>(swipe));
    } else if (QGesture *pan = event->gesture(Qt::PanGesture)) {
    panTriggered(static_cast<QPanGesture *>(pan));
    }
    return true;
    }

    void MyWidget::swipeTriggered(QSwipeGesture* gesture)
    {
    // Omitted for shorter code paste.
    }

    void MyWidget::panTriggered(QPanGesture* gesture)
    {
    // Omitted for shorter code paste.
    }
    @

    And I create the above widget inside a QMainWindow. When I run this on device I never get an event type == "QEvent::Gesture" which pretty much comes down to my problem, why does not this work?

    If I also do:
    grabGesture(Qt::TapGesture);
    Then I actually start to get events with type "QEvent::Gesture" in the (overloaded) event function. This leads me to believe that pan and swipe is not supported on the N8 but tap is? Could this be the case?

    In the end I've actually managed to get the pan gesture to trigger by sliding two fingers across the N8 screen simultaneously. Is that how it should be? It says nothing about that in the documentation. In the docs ("http://doc.qt.nokia.com/4.7/qpangesture.html":http://doc.qt.nokia.com/4.7/qpangesture.html) it actually shows an image of one finger so I suppose that is how it should work?

    But the swipe gesture is still not working, or maybe there is some special undocumented finger combination that needs to be used for that as well?

    Hope someone can clarify this.

    Best Regards,
    Johannes Petersson





  • Hi and thanks for the reply.

    That works but it does not really do what I want, the tap-gesture actually works out of the box on the N8 device. It is pan and swipe (mostly swipe since pan actually works with 2 fingers) that I can not get to work.



  • The swipe gesture will not work on the N8 because the device only has 2 touch points whereas if you look at the implementation of QSwipeGesture in qstandardgestures.cpp you will see that this gesture requires 3 touch points to be recognized.

    The pan gesture requires 2 touch points as you have already discovered.

    If you want to change this behavior, you most likely have to add a custom gesture recognizer.



  • Hi, thank you very much for the info! Then I know what to expect, shouldn't this be documented somewhere?

    Do you know if there is a way to test the gestures on Windows or Linux desktop? Maybe by holding ctrl or alt or something while clicking and dragging mouse?



  • Yes, the documentation does seem to be lacking in this area. I recommend creating a bug report for this on bugreports.qt.nokia.com.

    Using gestures on Windows or Linux is possible, but it requires that the actual OS and hardware supports touch events. I don't think there is any code in Qt to "fake" this using a keyboard modifier. There probably exists some hack somewhere on the net where you could plug in two mice and somehow enable multiple touch points :)



  • In a similar vein, and using nearly identical code to that above, i'm trying to get my N8 to recognize a long press (QTapAndHoldGesture). I've tested this on Windows using the simulator and it works just fine with no activity until the Hold expires. However, when I try this on the N8 the timer never seems to fire (QT 4.6.x by the way). Here's the behavior i'm experiencing:

    If I lift off before the Hold period expires I receive the cancel.
    If I lift off after the Hold period expires I receive the gesture
    If I don't lift off then nothing happens
    If I move out of the hot spot i receive a cancel - Then a started & finish event as I lift off.

    Given this works on the simulator (qt 4.7.x) I'm guess it's a problem with Qt 4.6? Any help would be gratefully appreciated


Log in to reply
 

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