Really tight pinch-zoom-scroll on QGraphicsView?



  • Hello! Has anyone ever seen a super tight two-fingered pinch-zoom-scroll gesture implementation on QGraphicsView? This is such an important gesture and I have never really seen it done very well in Qt with the graphics view framework.

    Thanks!



  • Hi @patrickkidd

    Here is a piece of code that zooms/scrolls my QGraphicsView well, but I don't know how is "really tight" measured so there is a variable reallyTight that covers that.

    bool MyGraphicsView::viewportEvent(QEvent* event) {
        if (event->type() == QEvent::TouchBegin || event->type() == QEvent::TouchUpdate || event->type() == QEvent::TouchEnd) {
          QTouchEvent *te = static_cast<QTouchEvent*>(event);
          if (te->touchPoints().count() == 1) {
              // single touch routines
          } else if (te->touchPoints().count() == 2) {
            QLineF l1(te->touchPoints()[0].startPos(), te->touchPoints()[1].startPos()); // initial double-touch position
            QLineF l2(te->touchPoints()[0].pos(), te->touchPoints()[1].pos()); // final position
            qreal distance = l1.length() - l2.length();
            if (distance < -reallyTight) { 
                // zoom out
            } else if (distance > reallyTight) { 
                // zoom in
            } else if (distance < reallyTight * 0.75) // just scroll
              verticalScrollBar()->setValue(verticalScrollBar()->value() + int(te->touchPoints()[0].lastPos().y() - te->touchPoints()[0].pos().y()));
            return true;
          }
        }
      return QGraphicsView::viewportEvent(event);
    }
    

    I hope it may be sort of start point.



  • @SeeLook said in Really tight pinch-zoom-scroll on QGraphicsView?:

    Hi @patrickkidd

    Here is a piece of code that zooms/scrolls my QGraphicsView well, but I don't know how is "really tight" measured so there is a variable reallyTight that covers that.

    bool MyGraphicsView::viewportEvent(QEvent* event) {
        if (event->type() == QEvent::TouchBegin || event->type() == QEvent::TouchUpdate || event->type() == QEvent::TouchEnd) {
          QTouchEvent *te = static_cast<QTouchEvent*>(event);
          if (te->touchPoints().count() == 1) {
              // single touch routines
          } else if (te->touchPoints().count() == 2) {
            QLineF l1(te->touchPoints()[0].startPos(), te->touchPoints()[1].startPos()); // initial double-touch position
            QLineF l2(te->touchPoints()[0].pos(), te->touchPoints()[1].pos()); // final position
            qreal distance = l1.length() - l2.length();
            if (distance < -reallyTight) { 
                // zoom out
            } else if (distance > reallyTight) { 
                // zoom in
            } else if (distance < reallyTight * 0.75) // just scroll
              verticalScrollBar()->setValue(verticalScrollBar()->value() + int(te->touchPoints()[0].lastPos().y() - te->touchPoints()[0].pos().y()));
            return true;
          }
        }
      return QGraphicsView::viewportEvent(event);
    }
    

    I hope it may be sort of start point.

    Thanks for that, I should have been more specific when I said "tight." I would like to implement the sort of simultaneous two-fingered zoom and scroll that is so common in iOS apps, for example when you are looking at a photo in the Photos app. You can pinch and zoom and scroll into any place on the photo, and it is perceptively almost perfectly "tight" in that it sticks to where your fingers are and is quite fast.



  • @patrickkidd
    I've never used it so far but I believe this piece of code could be extended easily to achieve such gesture.


Log in to reply
 

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