QGraphicsView滚动问题 [英] QGraphicsView scroll problem

查看:2190
本文介绍了QGraphicsView滚动问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有以下情况:我在一个图形场景中做一些绘图,在一种类型的中央窗口小部件,有时候这也涉及滚动场景。然后用户单击应用程序中的其他位置,中心窗口小部件更改为其他位置(并且图形视图的滚动条位置保存在某处)。现在,用户想要回到图形场景,我重新绘制场景(通过重新创建整个图形视图窗口,因为当用户在图形场景之外时,他改变了一些必须在图形视图中改变的值) ,当我想设置以前的滚动位置,我调用以下方法:

I have the following situation here: I do some drawing on a graphics scene, in one type of central widget, and some times this also involves scrolling the scene. Then the user clicks somewhere else in the application, the central widget is changed to something else (and the graphic views' scroll bar positions are saved somewhere). Now, the user wants to click back to the graphics scene, I redraw the scene (by recreating the entire graphics view window, because when the user was outside of the graphics scene he changed some values that must be changed in the graphics view too), and when I want to set the previous scrolling positions, I call the following method:

void GraphicsForm::scrollTo(int hor, int ver)
{
    m_qgv->horizontalScrollBar()->setValue(hor);
    m_qgv->verticalScrollBar()->setSliderPosition(ver);
}

并且这应该将滚动条重新定位到以前的值(m_qgv是一个QGraphicsView对象)。

and this is supposed to reposition the scroll bar to the previous values (hor, ver) saved before I changed the view (m_qgv is a QGraphicsView object).

问题是,水平和垂直值都没有被正确还原在隐藏场景之前使用 horizo​​ntal / verticalScrollBar() - > sliderPosition()正确保存,因此滚动条不会像在窗口之前那样移动

The issue is, the neither the horizontal, nor the vertical values are being reverted correctly (they are saved correctly using the horizontal/verticalScrollBar()->sliderPosition() before hiding the scene), so the scroll bars do not move as much as they were before the window change, they move much more less...

任何想法,建议如何还原到上一个滚动坐标?

Any ideas, suggestions how can I revert to the previous scroll coordinates?

推荐答案

如果用户编辑后场景rect改变,则保存的滚动位置将不再有效,因为新的最小值/最大值不同。

If the scene rect changes after user edit, the saved scroll position will no longer be valid since the new min/max are now different.

重现场景可能是更好的解决方案:

Recentering the scene may be a better solution:

QPointF center = my_qgv->viewPort().rect().center();
center = my_qgv->mapToScene(center);
...
// user edit; reconstruct scene
//
my_qgv->centerOn(center);

这篇关于QGraphicsView滚动问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆