Important: Please read the Qt Code of Conduct -

Touch zoom in QGraphicsView

  • Hello!

    I want to implement a touch zoom in a QGraphicsView, but I have no idea to how to do it.
    Anybody knows how to do it?

    Thank you very much!

  • @ivanicy I guess a good starting point is this post, which shows zoom on mouse wheel movement. Then you may want to try the zoom control implemented in a QML camera example.

    Good zooming!

  • Finally I could do it following this example:

  • Hello again!!

    I need some help with the touch zoom.

    I need to zoom in more smoothly, because it is very sensitive. Here is the code that I have:

    bool Graphics_view_zoom::eventFilter(QObject *object, QEvent *event) {
        if (event->type() == QEvent::MouseMove) {
            // code for mouse event
        } else if (event->type() == QEvent::Wheel) {
            // code for wheel event
        }   else if (event->type() == QEvent::TouchBegin || event->type() == QEvent::TouchUpdate || event->type() == QEvent::TouchEnd) {
            QTouchEvent *touchEvent = static_cast<QTouchEvent *>(event);
            QList<QTouchEvent::TouchPoint> touchPoints = touchEvent->touchPoints();
            if (touchPoints.count() == 2) {
                // determine scale factor
                const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
                const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
                qreal currentScaleFactor = QLineF(touchPoint0.pos(), touchPoint1.pos()).length() / QLineF(touchPoint0.startPos(), touchPoint1.startPos()).length();                     
                if (touchEvent->touchPointStates() & Qt::TouchPointReleased) {
                    // if one of the fingers is released, remember the current scale
                    // factor so that adding another finger later will continue zooming
                    // by adding new scale factor to the existing remembered value.
                    //currentScale *= currentScaleFactor;
                    currentScaleFactor = 1;
            return true;
        return false;

    And the zoom function:

    void Graphics_view_zoom::gentle_zoom(double factor) {
        if (currentScale * factor < scaleMin) {
            _view->fitInView(_view->scene()->sceneRect(), Qt::KeepAspectRatio);
            emit zoomed(false);
        else {
        _view->scale(factor, factor);
        currentScale *= factor;
        QPointF delta_viewport_pos = target_viewport_pos - QPointF(_view->viewport()->width() / 2.0,
                                                                   _view->viewport()->height() / 2.0);
        QPointF viewport_center = _view->mapFromScene(target_scene_pos) - delta_viewport_pos;
        emit zoomed(true);

    How can I do it more smoothly? It does a big zoom with a minimal gesture.

    Thank you very much!

Log in to reply