2016-09-21 57 views
0

我正在嘗試將Pyqtgraph嵌入到PyQt4 GraphicsView小部件中。我收到以下代碼的錯誤:我究竟做錯了什麼?錯誤將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 

回答

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