Qt中实现fitInView()后无法放大

发布于 2025-01-20 03:33:22 字数 1998 浏览 0 评论 0原文

我正在从事QT项目,而我要做的就是在后台拥有图像(可以是PNG或JPG)。

我创建了一个带有Qgraphicsview和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);

我将获得以下输出: “在此处输入图像描述”

但我希望整个图像在保持纵横比的同时适合视图。因此,我制作了一个从QgraphicsView继承的自定义类,并写下了以下内容:

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

现在,我得到以下输出: < img src =“ https://i.sstatic.net/zvv​​vt.png” alt =“在此处输入图像说明”>

这是可取的,但我现在无法放大视图。我只能放大。 PS-我写了一个鼠标Wheelevent函数以放大和缩小。

如何在设施中实施缩放?

编辑:这就是我实现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
}

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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放赐 2025-01-27 03:33:22

我遇到了同样的问题。 fitinview中的某些内容使变换中的缩放比例的上限。如果在缩放时打印qgraphicsview :: transform(),则会发现M11和M22的值不能大于1(调用FitinView之后)。

解决方案1:在FitinView之后致电RESETTRANSFOM()。不推荐,因为它重置打电话。

解决方案2:不要使用FitinView,而是写自己的FitinView版本。

请注意,在我的实施中,我知道我在场景中只有一个项目。我还更改了场景的矩形大小,这对于其他人来说可能不是必需的。 ImageViwer是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);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文