【问题标题】:How do I draw a plot using pyqtgraph on PyQt4 widget created in QT Designer?如何在 QT Designer 中创建的 PyQt4 小部件上使用 pyqtgraph 绘制绘图?
【发布时间】:2025-12-15 22:45:02
【问题描述】:

我只是从 pyqtgraph 开始。我有一个 graphicsView 小部件,我根据文档使用 QT 设计器进行了推广。我想尝试一个情节,看看它是否有效。当我尝试pg.plot(x,y) 时,程序在单独的窗口中而不是在 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

【问题讨论】:

    标签: python pyqt4 pyqtgraph


    【解决方案1】:

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

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

    【讨论】: