问题
I use pyqtgraph
to plot in PyQt5 GUI. I can plot in a pop-up window, but I am trying embed the graph in the graphicsView
area.
Here is how I can do a simple plot in a new window:
self.pushButton.clicked.connect(self.btn_clk)
MainWindow.show()
def btn_clk(self):
L = [1,2,3,4,5]
pg.plot(L)
I tried to use this line to embed the plot, but it doesn't work:
self.pushButton.clicked.connect(self.btn_clk)
MainWindow.show()
def btn_clk(self):
L = [1,2,3,4,5]
self.graphicsView.plot(L)
Here is the full code:
import pyqtgraph as pg
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(662, 512)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget)
self.horizontalLayout.setObjectName("horizontalLayout")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setObjectName("lineEdit")
self.verticalLayout.addWidget(self.lineEdit)
self.graphicsView = QtWidgets.QGraphicsView(self.centralwidget)
self.graphicsView.setObjectName("graphicsView")
self.verticalLayout.addWidget(self.graphicsView)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setObjectName("pushButton")
self.verticalLayout.addWidget(self.pushButton)
self.horizontalLayout.addLayout(self.verticalLayout)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 662, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
self.pushButton.clicked.connect(self.btn_clk)
MainWindow.show()
def btn_clk(self):
L = [1,2,3,4,5]
pg.plot(L)#this line plots in a new window
self.graphicsView.plot(L)#this line doesn't work
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
回答1:
Pg.plot returns an instance of the pg.graphicsWindows.PlotWindow
class, and this class inherits from QGraphicsView
, ie pg.graphicsWindows.PlotWindow
is used to plot but QGraphicsView
is not. If you want to embed a plot within the Widget you must use the class pg.PlotWidget()
, you must change:
self.graphicsView = QtWidgets.QGraphicsView(self.centralwidget)
to:
self.graphicsView = pg.PlotWidget(self.centralwidget)
and:
def btn_clk(self):
L = [1,2,3,4,5]
self.graphicsView.plot(L)#this line doesn't work
Output:
来源:https://stackoverflow.com/questions/44718779/embeding-plot-into-graphicsview-in-pyqt5