我试图将pyqtgraph嵌入到PyQt4 GraphicsView小部件中。我收到以下代码的错误:我做错了什么?
#imports
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, pg):
vb = pg.ViewBox()
def __init__(self):
super(self.__class__, self).__init__()
self.setupUi(self) # This is defined in ui_pumptest.py file automatically
self.graphicsView.setCentralItem(self.vb) #set central item to be graph
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
错误是:
QWidget: Must construct a QApplication before a QPaintDevice
答案 0 :(得分:1)
需要使用此代码修复的两件事: (1) 从基类列表中删除pg,不从整个pyqtgraph库继承:
class Gui(QtGui.QMainWindow, ui_test.Ui_MainWindow, pg):
到
class Gui(QtGui.QMainWindow, ui_test.Ui_MainWindow):
(2) 在 init :
中构建视图框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.vb = pg.ViewBox()
self.graphicsView.setCentralItem(self.vb) #set central item to be graph