主要问题:根据是否显示GUI,QGraphicsView.mapToScene
方法返回不同的答案。为什么,我能绕开它吗?
上下文是我正在尝试编写单元测试,但我不想实际显示测试工具。在
下面的小例子说明了这种行为。我使用一个子类视图,通过调用mapToScene
,在场景坐标系中打印鼠标单击事件的位置,原点位于左下角(垂直比例为-1)。但是,mapToScene
在显示对话框之前没有返回我期望的结果。如果运行底部的main部分,会得到以下输出:
Size is (150, 200)
Putting in (50, 125) - This point should return (50.0, 75.0)
Before show(): PyQt5.QtCore.QPointF(84.0, -20.0)
After show() : PyQt5.QtCore.QPointF(50.0, 75.0)
在show()
之前,x上有34个像素的一致偏移量,y方向有105个像素点的偏移量(在y轴上偏移量反向移动,就好像没有应用比例一样)。这些偏移量看起来很随机,我不知道它们是从哪里来的。在
下面是示例代码:
^{pr2}$这个例子在Windows上的PyQt5中,但是在Linux上的PyQt4也有同样的作用。在
跳入C++ QT源代码时,QQ:Q/P>>
关键的是
p.rx() += d->horizontalScroll();
和垂直滚动。QGraphicsView始终包含滚动条,即使它们总是关闭或不显示。在显示小部件之前观察到的偏移量来自初始化时水平和垂直滚动条的值,当显示小部件和计算布局时,必须修改这些值以匹配视图/视口。为了使mapToScene
正常工作,必须设置滚动条以匹配场景/视图。在如果我把下面几行放在示例中对
^{pr2}$mapToScene
的调用之前,那么我将得到适当的转换结果,而不必显示小部件。在要更一般地执行此操作,可以从视图中提取一些相关的转换。在
这是一个既老套又难看的解决方案,因此,虽然它确实有效,但可能有一种更优雅的方法来处理这个问题。在
您是否尝试过实现自己的qgraphicsview并重写resizeEvent?当您在mapTo“something”中捣乱时,您必须处理好您的resizeEvents,请看一下我从您的代码中提取并稍加修改的代码>
这是你想要的行为吗?”)
相关问题 更多 >
编程相关推荐