4

我正在使用 QGraphicsView 缩放标签上的图像。但是当我缩小时,我想为缩小设置一个特定的限制。我正在使用以下代码

scene = new QGraphicsScene(this);
view = new QGraphicsView(label);
QPixmap pix("/root/Image);
Scene->addPixmap(pixmap);
view->setScene(scene);
view->setDragMode(QGraphicsView::scrollHandDrag);

在车轮事件的插槽中

void MainWindow::wheelEvent(QWheelEvent *ZoomEvent)
{
    view->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
    double scaleFactor = 1.15;
    if(ZoomEvent->delta() >0)
    {
        view->scale(scaleFactor,scaleFactor);
    }
    else
    {
        view->scale(1/scaleFactor,1/scaleFactor);
    }
}

我希望图像在一定程度上不应该缩小。我应该怎么办?我尝试为 QGraphicsView 设置最小尺寸,但这没有帮助。

谢谢你 :)

4

3 回答 3

8

也许这样的事情会有所帮助:

void MainWindow::wheelEvent(QWheelEvent *ZoomEvent)
{
    view->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
    static const double scaleFactor = 1.15;
    static double currentScale = 1.0;  // stores the current scale value.
    static const double scaleMin = .1; // defines the min scale limit.

    if(ZoomEvent->delta() > 0) {
        view->scale(scaleFactor, scaleFactor);
        currentScale *= scaleFactor;
    } else if (currentScale > scaleMin) {
        view->scale(1 / scaleFactor, 1 / scaleFactor);
        currentScale /= scaleFactor;
    }
}

如您所见,这个想法是缓存当前的比例因子,并且在它小于某个限制的情况下不要缩小。

于 2013-10-21T14:26:58.013 回答
2

这是另一种实现思路,其中当前缩放因子是从视图的变换矩阵中获得的:

void View::wheelEvent(QWheelEvent *event) {
   const qreal detail = QStyleOptionGraphicsItem::levelOfDetailFromTransform(transform());
   const qreal factor = 1.1;

   if ((detail < 10) && (event->angleDelta().y() > 0)) scale(factor, factor);
   if ((detail > 0.1) && (event->angleDelta().y() < 0)) scale((1 / factor), (1 / factor));
}
于 2018-06-28T01:41:31.653 回答
1

解决问题的另一种方法:

void GraphWidget::wheelEvent(QWheelEvent *event)
{
    int scaleFactor = pow((double)2, - event->delta() / 240.0);

    qreal factor = transform().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
    if (factor < 0.5 || factor > 20)
        return;
    scale(scaleFactor, scaleFactor);
}
于 2018-04-14T07:32:18.993 回答