【问题标题】:Error Embedding Pyqtgraph into PyQt4 GraphicsView将 Pyqtgraph 嵌入 PyQt4 GraphicsView 时出错
【发布时间】:2017-01-30 02:08:15
【问题描述】:

我正在尝试将 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

【问题讨论】:

    标签: python pyqt4 qgraphicsview pyqtgraph


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 2019-03-03
      • 2019-09-18
      • 2019-06-11
      • 2018-08-19
      • 2016-06-24
      • 2017-06-08
      • 2017-10-25
      • 2020-08-16
      相关资源
      最近更新 更多