使用pyQtGraph在pyqt graphicsView中绘制图像

时间:2016-03-10 02:59:22

标签: python pyqt4 pyqtgraph

我尝试对designerExample进行逆向工程以使其与绘图完成相同的操作,但是对于图像却得到两个结果,但都不正确:

  1. 如果我将QGraphicsView提升为GraphicsView,我会获得图片 输出,但它不是你想象的可移动或可缩放的 pyQtGraph中的项目(运行examples脚本 - 我期待与 GraphicsLayout 示例类似的行为:此示例中的网格中有一个可移动的图像。我的不是虽然我的代码基于这个例子)
  2. enter image description here

    enter image description here

    1. 如果我将QGraphicsView提升为GraphicsLayoutWidget,我会得到 AttributeError: 'QGraphicsView' object has no attribute 'addItem' 我可以在调试器中看到。但是,我看不到的是 debugger是the GraphicsLayoutWidget should have的GraphicsView属性。此外,GraphicsView也是 没有addItem属性,至少不是根据我的 我可以看到我在调试器中导航到ui.graphicsView的时候 断点设置为在调用addItem(img)
    2. 之前

      代码:

      import sys, os
      import pyqtgraph as pg
      import numpy as np
      from PyQt4.QtGui import *
      
      ## Define main window class from template
      path = os.path.dirname(os.path.abspath(__file__))
      uiFile = os.path.join(path, 'pyQtGraphTest.ui')
      WindowTemplate, TemplateBaseClass = pg.Qt.loadUiType(uiFile)
      
      class QMainWindow(TemplateBaseClass):
          def __init__(self):
              #super(QtGui.QMainWindow, self).__init__(parent)
              TemplateBaseClass.__init__(self)
      
              self.ui = WindowTemplate()
              self.ui.setupUi(self)
              self.ui.pushButton.clicked.connect(self.do_it)
      
          def do_it(self):
              frame = np.random.normal(size=(100,100))
              img = pg.ImageItem(frame)
              self.ui.graphicsView.addItem(img) #BREAKPOINT HERE
      
      
      if __name__ == '__main__':
          app = QApplication(sys.argv)
      
          form = QMainWindow()
          form.show()
          app.exec_()
      

      pyQtGraphTest.ui

      <?xml version="1.0" encoding="UTF-8"?>
      <ui version="4.0">
       <class>MainWindow</class>
       <widget class="QMainWindow" name="MainWindow">
        <property name="geometry">
         <rect>
          <x>0</x>
          <y>0</y>
          <width>632</width>
          <height>614</height>
         </rect>
        </property>
        <property name="windowTitle">
         <string>MainWindow</string>
        </property>
        <widget class="QWidget" name="centralwidget">
         <widget class="QPushButton" name="pushButton">
          <property name="geometry">
           <rect>
            <x>10</x>
            <y>10</y>
            <width>99</width>
            <height>27</height>
           </rect>
          </property>
          <property name="text">
           <string>PushButton</string>
          </property>
         </widget>
         <widget class="GraphicsView" name="graphicsView">
          <property name="geometry">
           <rect>
            <x>20</x>
            <y>50</y>
            <width>571</width>
            <height>501</height>
           </rect>
          </property>
         </widget>
        </widget>
       </widget>
       <customwidgets>
        <customwidget>
         <class>GraphicsView</class>
         <extends>QGraphicsView</extends>
         <header>pyqtgraph.h</header>
        </customwidget>
       </customwidgets>
       <resources/>
       <connections/>
      </ui>
      

1 个答案:

答案 0 :(得分:0)

回到您引用的示例GraphicsLayout,它们执行的事件序列是:

  1. 创建GraphicsView
  2. 创建GraphicsLayout
  3. 将GraphicsLayout对象设置为GraphicsView中心项
  4. 通过GraphicsLayout.addViewBox获取ViewBox引用
  5. 制作图片
  6. 将图像添加到ViewBox
  7. 您的代码省略了步骤2到4,并且您将图像直接添加到GraphicsView(因为您没有创建ViewBox,图像想要在哪里)。

    例如,如果您决定在Designer中推广GraphicsLayoutWidget,那也可以,但是您应该参考Plotting.py示例以获取添加图像的正确序列。 Plotting.py示例首先调用pg.GraphicsWindow(),它是GraphicsLayoutWidget的子类。

    以下是运行示例所需的修补程序。

    class QMainWindow(TemplateBaseClass):
    def __init__(self):
        #super(QtGui.QMainWindow, self).__init__(parent)
        TemplateBaseClass.__init__(self)
    
        self.ui = WindowTemplate()
        self.ui.setupUi(self)
        self.ui.pushButton.clicked.connect(self.do_it)
        self.glayout = pg.GraphicsLayout()
        self.vb = self.glayout.addViewBox()
    
    def do_it(self):
        frame = np.random.normal(size=(100,100))
        img = pg.ImageItem(frame)
        self.ui.graphicsView.setCentralItem(self.glayout)
        self.vb.addItem(img)
        self.vb.autoRange()
        #self.ui.graphicsView.addItem(img) #BREAKPOINT HERE
    
相关问题