Qt QGraphicsView 使用左上角作为缩放原点

发布于 2024-12-06 10:41:02 字数 1049 浏览 4 评论 0原文

我如何获取通过 QGraphicsView 看到的左上角并在缩放时保持其左上角?

因此,如果我在缩放时碰巧在左上角有符号“A”,则 A 将保留在那里但仍会缩放。目前以屏幕中心为刻度原点。但我希望左上角成为转变的原点。

也就是说,在图形视图中看到的左上角,而不是整个图形场景的左上角。 我该怎么做?

这是我的缩放代码,用于将场景缩放到视口宽度的 100%:

void GraphicsView::resizeEvent(QResizeEvent *event)
{
    QGraphicsView::resizeEvent(event);

    double scale_delta = (double) event->size().width() / scene()->width();
    resetMatrix();

    scale(scale_delta, scale_delta);
}

解决方案

void GraphicsView::resizeEvent(QResizeEvent *event)
{
    QGraphicsView::resizeEvent(event);

    if (!first_shown)
    {
        centerOn(0, 0);
        first_shown = true;
    }

    QPointF topleft = mapToScene(viewport()->rect().topLeft());

    resetMatrix();

    QPointF shift = (mapToScene(viewport()->rect().bottomRight() + QPoint(1, 1)) - mapToScene(viewport()->rect().topLeft()));
    shift /= (double) event->size().width() / scene()->width();

    fitInView(QRectF(topleft, topleft + shift));
}

How would I take the topleft corner as seen through the QGraphicsView and keep it the topleft while scaling?

So if I happen to have the symbol 'A' in the topleft corner while scaling, the A will stay there yet scales. At present the center of the screen is taking as the scale origin. But I would like the topleft corner be the origin for transformation.

That is, topleft as seen in the graphics view, not of the total graphics scene.
How would I do this?

This is my scale code to scale the scene at 100% of it's width to the viewport:

void GraphicsView::resizeEvent(QResizeEvent *event)
{
    QGraphicsView::resizeEvent(event);

    double scale_delta = (double) event->size().width() / scene()->width();
    resetMatrix();

    scale(scale_delta, scale_delta);
}

SOLUTION

void GraphicsView::resizeEvent(QResizeEvent *event)
{
    QGraphicsView::resizeEvent(event);

    if (!first_shown)
    {
        centerOn(0, 0);
        first_shown = true;
    }

    QPointF topleft = mapToScene(viewport()->rect().topLeft());

    resetMatrix();

    QPointF shift = (mapToScene(viewport()->rect().bottomRight() + QPoint(1, 1)) - mapToScene(viewport()->rect().topLeft()));
    shift /= (double) event->size().width() / scene()->width();

    fitInView(QRectF(topleft, topleft + shift));
}

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

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

发布评论

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

评论(1

稳稳的幸福 2024-12-13 10:41:03

如果我正确理解你的问题,你需要的基本步骤是:

  1. 将 Qt 的原点想法翻译到你想要缩放的位置,即左上角
  2. 然后应用缩放
  3. 然后再次翻译回来

If I understand your question correctly, the basic steps you'll need are:

  1. Translate Qt's idea of the origin to where you want to scale around, i.e. top-left
  2. Then apply the scale
  3. Then translate back again
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文