Qt 如何使用Qpinch手势缩放QGraphicsView的内容?

Qt 如何使用Qpinch手势缩放QGraphicsView的内容?,qt,touch,qgraphicsview,qgraphicsscene,Qt,Touch,Qgraphicsview,Qgraphicsscene,我正在嵌入式平台上实现一个图像查看器。硬件是一种平板电脑,有一个触摸屏作为输入设备。我使用的Qt版本是5.4.3 QGraphicsView用于显示包含QGraphicsPixmapItem的QGraphicscene。QGraphicsPixmapItem包含要显示的pixmap 守则的有关部分如下: void MyGraphicsView::pinchTriggered(QPinchGesture *gesture) { QPinchGesture::ChangeFlags changeFl

我正在嵌入式平台上实现一个图像查看器。硬件是一种平板电脑,有一个触摸屏作为输入设备。我使用的Qt版本是5.4.3

QGraphicsView用于显示包含QGraphicsPixmapItem的QGraphicscene。QGraphicsPixmapItem包含要显示的pixmap

守则的有关部分如下:

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());
}
void MyGraphicsView::pinchTriggered(qpinch手势*手势)
{
Qpinch手势::ChangeFlags ChangeFlags=手势->ChangeFlags();
if(changeFlags&Qpinch手势::ScaleFactorChanged){
currentStepScaleFactor=手势->totalScaleFactor();
}
如果(手势->状态()==Qt::手势完成){
scaleFactor*=currentStepScaleFactor;
currentStepScaleFactor=1;
返回;
}
//根据当前收缩级别计算比例因子
qreal sxy=scaleFactor*currentStepScaleFactor;
//获取指向当前显示图片的指针
QList listOfItems=items();
QGraphicsItem*item=listOfItems.at(0);
//缩放图片
项目.设置刻度(sxy);
//使场景适应缩放的图片
setSCEN竖立(场景()->itemsBoundingRect());
}
收缩后,将从视图的左上角开始缩放pixmap

如何缩放相对于Qpinch手势中心的pixmap?

该项将围绕其变换原点进行缩放,默认情况下,变换原点为(0,0)。通过调用setTransformOriginPoint(),可以选择不同的转换原点

该函数接受一个QPoint,因此您需要先找到中心点,然后设置原点

void QGraphicsItem::setTransformOriginPoint(const QPointF & origin)

我尝试在setScale之前执行setTransformOriginPoint,指定为点:手势->中心点()。结果不是预期的。pixmap仍按左上方向缩放。是否需要将手势->中心点()返回的QPointF映射到另一个坐标系?