开发者

Qt QGraphicsView use topleft as scale origin

开发者 https://www.devze.com 2023-04-08 16:40 出处:网络
How would I take the topleft corner as seen through the QGraphicsView and keep it the topleft while scaling?

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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号