Search code examples
c++qtqt-creatorqgraphicsviewqgraphicsscene

Can't zoom in after implementing fitInView() in Qt


I'm working on a project in Qt and the thing I've to do is to have an image in the background(can be png or jpg).

I've created a view with a scene with QGraphicsView and QGraphicsScene.

QGraphicsPixmapItem * image = new QGraphicsPixmapItem(QPixmap("...../world_map.png"));
int imageWidth = image->pixmap().width();
int imageHeight = image->pixmap().height();
image->setOffset(- imageWidth / 2, -imageHeight / 2);
image->setPos(0, 0);

QGraphicsScene *scene = new QGraphicsScene();
scene->setSceneRect(-imageWidth / 2, -imageHeight / 2, imageWidth, imageHeight);
QGraphicsView * gv = new QGraphicsView();
gv->setScene(scene);
gv->scene()->addItem(image);

I get the following output:enter image description here

But I wanted the whole image to fit the view while maintaining the aspect ratio. So, I made a custom class inherited from QGraphicsView and wrote the following:

void MyView::resizeEvent(QResizeEvent *event)
{
    QGraphicsView::resizeEvent(event);
    fitInView(sceneRect(), Qt::KeepAspectRatio);
}

Now, I got the following output:enter image description here

This is desirable but I can't zoom in on the view now. I can only zoom out. P.S. - I wrote a mouseWheelEvent function to zoom in and out.

What can be done to implement zooming in facility??

Edit : This is how I implemented zoom in/out:

void MyView::wheelEvent(QWheelEvent *e)
{
    static const double factor = 1.1;
    static double currentScale = 1.0;
    static const double scaleMin = 1.0;
    ViewportAnchor oldAnchor = transformationAnchor();

    setTransformationAnchor(QGraphicsView::AnchorUnderMouse); // set focus to mouse coords

    //if (e->delta() > 0)
    if (e->angleDelta().y() > 0){
        scale(factor, factor);
        currentScale *= factor;
    }
    else if (currentScale > scaleMin){
        scale(1 / factor, 1 / factor);
        currentScale /= factor;
    }
    setTransformationAnchor(oldAnchor); // reset anchor
}

Solution

  • I encountered the same problem. Something in fitInView makes the upper limit of scaling in transform to 1. If you print QGraphicsView::transform() while scaling, you will find values of m11 and m22 cannot be greater than 1(after calling fitInView).

    Solution 1: call resetTransfrom() after fitInView. Not recommended because it resets calling.

    Solution 2: Don't use fitInView, I write my own version of fitInView instead.

    Note, in my implementation, I know I only have one item in the scene. I also change scene rect size, which may not be necessary for others. ImageViwer is child class of qgraphicsview.

    void ImageViewer::resizeEvent(QResizeEvent *) {
      QList<QGraphicsItem *> i = items();
      int window_w = width();
      if (window_w == 0 || i.size() != 1) return;
    
      auto *item = qgraphicsitem_cast<QGraphicsPixmapItem *>(i[0]);
      qreal img_w = static_cast<double>(item->pixmap().width());
      qreal factor = window_w / img_w;
      item->setScale(factor);
    
      QRectF rect = item->boundingRect();
      rect.setHeight(height());
      rect.setWidth(width());
      rect.moveCenter(item->boundingRect().center());
    
      QGraphicsScene *s = scene();
      s->setSceneRect(rect);
    
      item->setTransformOriginPoint(item->boundingRect().center());
      centerOn(item);
    }