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. [SOLVED] QSwipeGesture implementation on Desktop
QtWS25 Last Chance

[SOLVED] QSwipeGesture implementation on Desktop

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 4.4k 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.
  • V Offline
    V Offline
    vezprog
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vezprog
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #3

        [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.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0
        • V Offline
          V Offline
          vezprog
          wrote on last edited by
          #4

          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!

          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