Qt QGraphicsView 使用左上角作为缩放原点
我如何获取通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确理解你的问题,你需要的基本步骤是:
If I understand your question correctly, the basic steps you'll need are: