3
votes

I'm implementing an image viewer on an embedded platform. The hardware is a sort of tablet and has a touch screen as input device. The Qt version I'm using is 5.4.3.

The QGraphicsView is used to display a QGraphicsScene which contains a QGraphicsPixmapItem. The QGraphicsPixmapItem containts the pixmap to display.

The relevant part of the code is the following:

void MyGraphicsView::pinchTriggered(QPinchGesture *gesture)
{
QPinchGesture::ChangeFlags changeFlags = gesture->changeFlags();

if (changeFlags & QPinchGesture::ScaleFactorChanged) {
    currentStepScaleFactor = gesture->totalScaleFactor();
}

if (gesture->state() == Qt::GestureFinished) {
    scaleFactor *= currentStepScaleFactor;
    currentStepScaleFactor = 1;
    return;
}

// Compute the scale factor based on the current pinch level
qreal sxy = scaleFactor * currentStepScaleFactor;

// Get the pointer to the currently displayed picture
QList<QGraphicsItem *> listOfItems = items();
QGraphicsItem* item = listOfItems.at(0);

// Scale the picture
item.setScale(sxy);

// Adapt the scene to the scaled picture
setSceneRect(scene()->itemsBoundingRect());
}

As result of the pinch, the pixmap is scaled starting from the top-left corner of the view.

How to scale the pixmap respect to the center of the QPinchGesture?

1

1 Answers

0
votes

From The Docs

The item is scaled around its transform origin point, which by default is (0, 0). You can select a different transformation origin by calling setTransformOriginPoint().

That function takes in a QPoint so you would need to find out your centre point first then set the origin point.

void QGraphicsItem::setTransformOriginPoint(const QPointF & origin)