如何在没有缩放笔宽度的情况下在QGraphicsScene上绘图?

时间:2013-10-21 13:38:55

标签: qt qt4 pyqt pyqt4

我正在尝试绘制QGraphicScene数据,根据情况,这些数据可能会有数量级的变化。由于我使用的笔是化妆品,我希望视图独立于数据的大小。但我得到的是:

噪音乘以50000的正弦波:

scale 50000

噪音乘以50的正弦波:

scale 50

但是,如果我放大到这些图中的任何一个(两者中都有相同的缩放量),当两个图像看起来相同时,我最终会达到一个水平:

scale 50000 zoom enter image description here

这里发生了什么?为什么笔的宽度只是因为数据值更大而改变。为什么缩放时缩放会消失?

重现此代码的代码如下。左键单击绘图放大,右键单击缩小。

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_()

1 个答案:

答案 0 :(得分:0)

就像Mad Physicist告诉您的那样,将笔设置为装饰性或线宽为0(在行为上是相等的),并且您使用的是非缩放笔。

  
    

pen.setCosmetic(True);