【发布时间】:2013-10-30 00:57:36
【问题描述】:
我正在尝试绘制 QGraphicScene 数据,根据情况,这些数据可能会发生数量级的变化。由于我使用的笔是化妆品,我希望视图独立于数据的大小。但我得到的是:
噪声乘以 50000 的正弦波:
噪声乘以 50 的正弦波:
但是,如果我放大这些图中的任何一个(两者的放大倍数相同),我最终会达到两个图像看起来相同的水平:
这里发生了什么?为什么只是因为数据值更大,笔的宽度会发生变化。为什么放大后缩放会消失?
重现此的代码如下。左键放大,右键缩小。
import sys
from PyQt4 import QtGui as QG
from PyQt4 import QtCore as QC
import numpy as n
class ZoomView(QG.QGraphicsView):
"""Zoomable QGraphicsView"""
def mouseReleaseEvent(self,event):
if event.button() == QC.Qt.LeftButton:
self.scale(1.5,1)
elif event.button() == QC.Qt.RightButton:
self.scale(1/1.5,1)
class MainUI(QG.QDialog):
def __init__(self, parent=None):
super(MainUI, self).__init__(parent)
layout = QG.QVBoxLayout()
self.setLayout(layout)
button_layout = QG.QHBoxLayout()
pb3 = QG.QPushButton('add plot')
button_layout.addWidget(pb3)
layout.addLayout(button_layout)
pb3.clicked.connect(self.scene_maker_singleshot)
scene = QG.QGraphicsScene()
view = ZoomView(self)
view.setTransformationAnchor(QG.QGraphicsView.AnchorUnderMouse)
view.setRenderHint(QG.QPainter.Antialiasing)
layout.addWidget(view)
view.setScene(scene)
self.view = view
self.scene = scene
def scene_maker_singleshot(self):
"""Draw scene and fit in view"""
t1 = 50
t2 = 100
QC.QTimer.singleShot(t1, self.make_scene)
QC.QTimer.singleShot(t2, lambda: self.view.fitInView(self.view.sceneRect()))
def make_scene(self):
scale = 50
#scale = 50000
noise_amp = 0.2*scale
points = 1000
xdata = n.arange(points)
#generate sine data and random noise
ydata = n.sin(xdata/(points/10.))*scale +\
n.random.randint(noise_amp, size=points)
pen = QG.QPen(QG.QColor("red"))
for i in xrange(1, xdata.size):
self.scene.addLine(xdata[i-1], ydata[i-1], xdata[i], ydata[i], pen)
if __name__=="__main__":
app = QG.QApplication(sys.argv)
gui = MainUI()
gui.setFixedSize(500,500)
gui.show()
app.exec_()
【问题讨论】:
-
我无法重现这一点:两种秤对我来说都给出了完全相同的结果。这是在 Linux 上,使用 python 2.7.5、qt 4.8.5 和 pyqt 4.10.3。
-
啊。我在 OSX 中看到了这个。所以可能是平台特定的“功能”。我还发现这种行为仅在两个事件同时发生时才会发生:a)QGraphicsView 小部件的宽度小于正在绘制的点数,b)垂直轴上的绘图数据幅度更大比水平的
-
在 Linux 中尝试过。没问题。我想我将不得不在 OSX 中使用一种解决方法。感谢您测试代码!
-
在
pen = QG.QPen(QG.QColor("red"))之后,您可能需要明确声明pen.setCosmetic(True)。