I am working with qgis 2.8.2 c++ api and qt4.8.5 in my project i had to implement the pinchzoom because i use a 27" touch screen i am looking for information on how to use the qgis lib with the qt qtouchevent of Qt

does anyone have information ?

1 Answer 1


after a long work i find a solution in order to use the qtouchevent for zooming the map like a tablet you need to catch the viewportevent which is in the qgraphicview qt library

in qgis we use a qgsmapcanvas, and the qgsmapcanvas inherit of the qgraphicview of qt , so it is very easy to implement the touchevent

here is a sample code i use and works

bool CMyQgsMapCanvas::viewportEvent(QEvent *event)
    switch (event->type()) {
        case QEvent::TouchBegin:
        case QEvent::TouchUpdate:
        case 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();

            return true;
    return QGraphicsView::viewportEvent(event);

when i call the zoomFactor method perhaps you need to adjust the ratio for a more accurate zoom effect on your map.

don't forget to enabled the touchevent you must activate it in the constructor of your overload class of qggsmapcanvas by adding these lines :


that's all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.