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.


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

Log in to reply

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