如何在 QT Designer 中创建的 PyQt4 小部件上使用 pyqtgraph 绘制绘图?

How do I draw a plot using pyqtgraph on PyQt4 widget created in QT Designer?

我刚开始使用 pyqtgraph。我有一个 graphicsView 小部件,我根据文档使用 QT 设计器进行了推广。我想尝试一个情节,看看它是否有效。当我尝试 pg.plot(x,y) 时,程序在单独的 window 而不是在 graphicsView 小部件中创建了一个图。我正在使用 Windows 10、PyQt4 和 Python 2.7。我做错了什么?

from PyQt4 import QtGui
from PyQt4 import QtCore
import ui_test  #Gui File
import sys
import pyqtgraph as pg


class Gui(QtGui.QMainWindow, ui_test.Ui_MainWindow):


    def __init__(self):        
        super(self.__class__, self).__init__()        
        self.setupUi(self)  # This is defined in ui_pumptest.py file automatically   
        self.plot()

    def plot(self):       
        vb = pg.ViewBox()
        self.graphicsView.setCentralItem(vb)
def main():
    app = QtGui.QApplication(sys.argv)  # A new instance of QApplication
    form = Gui()  # We set the form to be our ExampleApp (design)
    form.show()  # Show the form
    app.exec_()  # and execute the. app

if __name__ == '__main__':  # if we're running file directly and not importing it
    main()  # run the main function

能否分享 ui_pumptest.py 文件源?否则很难说出你的意图是什么。如果没有,请至少详细说明您在 QtDesigner 的 QGraphicsView 提升过程中使用的构造(假设您遵循 http://www.pyqtgraph.org/documentation/how_to_use.html#embedding-widgets-inside-pyqt-applications)。

pg.plot 创建它自己的 plotwindow->plotwidget 结构,所以这就是为什么你在调用它时会得到一个单独的 window 的原因。您调用 plot on 的项目应该是在您的 Qt Ui 文件中提升的容器对象名称。