1

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
}
Prakhar
  • 49
  • 7
  • You should show how `mouseWheelEvent` was implemented. To make zoomIn/zoomOut it is enough to get current scaleX/Y value of view and multiply it by proper factor. – rafix07 Apr 09 '22 at 06:02
  • @rafix07 I have edited my question and added the wheelEvent – Prakhar Apr 09 '22 at 06:58
  • @rafix07 `QGraphicsView::scale(qreal sx, qreal sy)` scales the current view transformation by `(sx, sy)`. So the current view transformation is scaled every time `scale` is called. Also, zooming works fine when I remove `fitInView`. – Prakhar Apr 09 '22 at 08:57
  • there's something with `fitInView` which is preventing the zooming in (i.e increasing scale of current view transformation). Zoom out works fine in boths cases (i.e. with or without `fitInView`. – Prakhar Apr 09 '22 at 08:59
  • Srry for my previous comment, my bad. I was wrong. – rafix07 Apr 09 '22 at 09:04

1 Answers1

1

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);
}