Python类实例调用崩溃

时间:2013-06-04 19:35:58

标签: python class crash custom-component pyqtgraph

我正在使用pyqtgraph进行实时数据绘图。不幸的是,该库不支持时间序列实时绘图,所以我去google搜索并找到了为此编写特定类的人(你可以找到它here)。我一次使用这些类没有任何问题,但现在python在调用'TimeSeriesPlot()'类时立即崩溃。我试图删除python文件夹和所有子文件夹中的所有.pyc文件,但这没有改变任何东西,也没有修复python安装。有关导致问题的原因以及如何解决问题的任何想法?有没有人遇到过那种问题? 我的配置是:

  • Windows 7 Home Edition Blockquote
  • Python 2.7.5
  • Pyside 1.1.2 with QT 4.8
  • PyQtGraph 0.9.7

以下是我尝试执行的文件的内容:

from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg

class TimeSeriesPlotViewBox(pg.ViewBox):
    def __init__(self, timeSeriesPlot, *args, **kwds):
       pg.ViewBox.__init__(self, *args, **kwds)
       self.timeSeriesPlot = timeSeriesPlot


   def mouseClickEvent(self, ev):
      if ev.button() == QtCore.Qt.RightButton:

         self.timeSeriesPlot.xFrom = None
         self.timeSeriesPlot.xTo = None
         self.timeSeriesPlot.updateView()

         self.enableAutoRange(pg.ViewBox.XAxis,1.0)
         self.enableAutoRange(pg.ViewBox.YAxis,1.0)      


   def mouseDoubleClickEvent(self, ev):
      if ev.button() == QtCore.Qt.LeftButton:

         xFrom = 0
         if not self.timeSeriesPlot.xFrom is None and self.timeSeriesPlot.xFrom < 0:
            xFrom = self.timeSeriesPlot.xFrom 
         else:
            xFrom = -len(self.timeSeriesPlot.timedata)
         self.timeSeriesPlot.xFrom = int(round(xFrom / 2.0))


         self.timeSeriesPlot.updateView()
         self.enableAutoRange(pg.ViewBox.XAxis,1.0)
         self.enableAutoRange(pg.ViewBox.YAxis,1.0)    

   def mouseDragEvent(self, ev):
      if ev.button() == QtCore.Qt.RightButton:
         ev.ignore()
      else:
         pg.ViewBox.mouseDragEvent(self, ev)


class TimeSeriesPlot(pg.QtCore.QObject):

   def __init__(self, tsTitle, parent = None):
      pg.QtCore.QObject.__init__(self, parent)    

      self.vb =TimeSeriesPlotViewBox(self)

      self.plt = pg.PlotWidget(viewBox=self.vb, title = tsTitle)
      self.vb.sigRangeChangedManually.connect(self.zoom)

      #time axis
      self.timedata = []

      #val data
      self.valdata = []
      self.curveVal = pg.PlotDataItem([])
      self.plt.addItem(self.curveVal)

      self.xFrom = -50
      self.xTo = None

   def zoom(self):
       xlimits,ylimits = self.vb.viewRange()

       import bisect
       self.xFrom = bisect.bisect_left(self.timedata,xlimits[0])
       self.xTo = bisect.bisect_right(self.timedata,xlimits[1])

       self.vb.disableAutoRange(pg.ViewBox.XAxis)
       self.vb.disableAutoRange(pg.ViewBox.YAxis)

       self.updateView()

   def show(self):
       self.plt.show()

   def updateModel(self,newdata):
       time = float(newdata["time"])
       val = float(newdata["val"])

       self.timedata.append(time)
       self.valdata.append(val)

   def updateView(self):
       useAA = True
       viewSlice = None
       maxElementCnt = 500.0
       if self.xFrom is None and self.xTo is None:
          elementCnt = len(self.timedata)
          step = max(int(round(elementCnt / maxElementCnt)),1)
          viewSlice = slice(-elementCnt,None,step)
       elif self.xFrom < 0:
          elementCnt = -self.xFrom
          step = max(int(round(elementCnt / maxElementCnt)),1)
          viewSlice = slice(-elementCnt,None,step)
       else:
          elementCnt = self.xTo - self.xFrom
          step = max(int(round(elementCnt / maxElementCnt)),1)
          viewSlice = slice(self.xFrom,self.xTo,step)

       useAA = True    
       self.curveVal.setData(x=self.timedata[viewSlice],y=self.valdata[viewSlice],clear=True,antialias=useAA)
####################################
TimeSeriesPlot('plot')

1 个答案:

答案 0 :(得分:1)

我运行此代码时看到的第一条错误消息是:

QWidget: Must construct a QApplication before a QPaintDevice

所有Qt应用都是如此;您必须先创建一个QApplication实例。修复此问题后,脚本仍然会因分段错误而崩溃,可能是因为TimeSeriesPlot实例被立即删除(因为它未分配给任何变量)。即使在修复之后,脚本也会立即退出(因为没有别的事可做)。我用以下代码替换了示例中的最后一行以获得预期结果:

app = pg.QtGui.QApplication([])
tsp = TimeSeriesPlot('plot')
tsp.show()
app.exec_()