PyQt - PyQtGraph - > QWidget:必须在QPaintDevice

时间:2015-11-09 15:43:27

标签: qt python-3.x pyqt pyqtgraph

目前正在尝试创建一个嵌入PyQtGraph图的PyQt5应用程序, 我已经在我的概念验证中做到了,现在尝试升级它。

我使用GraphicsLayoutWidget将我的图表嵌入PyQt

但我有一条错误消息:

  

QWidget:必须在QPaintDevice之前构造QApplication。所以我读到了它,似乎我的QApplication没有创建。

但是我不明白,我的mainWindow正在运行,我尝试在Button Click事件上添加GraphicsLayoutWidget,所以我不明白为什么它不起作用。

此样本是效果良好的POC

if __name__ == "__main__":

# set default pyqtgraph color
pg.setConfigOption('background', 'w')
pg.setConfigOption('foreground', 'k')

app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)

global LeftPanelContent
LeftPanelContent = QtWidgets.QFrame()
LeftPanelContent.setMinimumSize(QtCore.QSize(200, 0))
LeftPanelContent.setMaximumSize(QtCore.QSize(200, 16777215))
LeftPanelContent.setObjectName("LeftPanelContent")

global PanelInfoPatient
PanelInfoPatient = QtWidgets.QFrame()
PanelInfoPatient.setMinimumSize(QtCore.QSize(250, 0))
PanelInfoPatient.setMaximumSize(QtCore.QSize(350, 16777215))
PanelInfoPatient.setObjectName("PanelInfoPatient")

#ui3 = Ui_MiddlePanel()
#ui3.setupUi(PanelInfoPatient)
ui3 = Ui_Form()
ui3.setupUi(PanelInfoPatient)
ui3.ButtonUpdate.clicked.connect(onUpdatePatientClicked)
ui3.ButtonDelete.clicked.connect(onDeletePatientClicked)


ui2 = Ui_LeftPanelContent()
ui2.setupUi(LeftPanelContent)
ui2.ButtonOrganisation.clicked.connect(showRightPanelCollapsible)
ui2.HideButton.clicked.connect(hideLeftPanels)

PanelRight = QtWidgets.QFrame()
PanelRight.setMinimumSize(QtCore.QSize(700, 0))
PanelRight.setObjectName("PanelRight") 

ui4 = Ui_RightPanel()
ui4.setupUi(PanelRight) 
ui4.widget_2 = pg.GraphicsLayoutWidget();
ui4.verticalLayout.addWidget(ui4.widget_2)
ui4.ButtonEvent.clicked.connect(onEventClicked);
ui4.ButtonDay.clicked.connect(onDayClicked);
ui4.ButtonWeek.clicked.connect(onWeekClicked);
ui4.ButtonMonth.clicked.connect(onMonthClicked);
ui4.widget_2.hide()

ui.gridLayout_2.addWidget(LeftPanelContent, 0, 0, 1, 1)
ui.gridLayout_2.addWidget(PanelInfoPatient, 0, 1, 1, 1)
ui.gridLayout_2.addWidget(PanelRight,  0, 2, 1, 1)


setMainWindowColor()
#MainWindow.setWindowFlags(QtCore.Qt.FramelessWindowHint)
MainWindow.show()
sys.exit(app.exec_())

现在新的实施:

Launcher.py:

def launchApp(app):
    appController = ApplicationController()
    appController.showMainWindow()
    return app.exec_()

if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    setLangage()
    result = launchApp(app)
    sys.exit(result)

MainWindows视图:

class MainWindow(QMainWindow, Ui_MainWindow, IMainWindows):

def __init__(self, parent=None):
    super(MainWindow, self).__init__(parent)
    self.setupUi(self)

    self.LeftPanelContent = None
    self.PanelInfoPatient = None
    self.PanelRight = None
    self.collaspedLeftPanel = None

    self.leftPanelUi = None
    self.panelInfoPatientUi = None
    self.rightPanelUi = None
    self.collaspedLeftPanelUi = None
    self.dialog = None
    self.lastDir = None

def guiAssemblage(self):

    pg.setConfigOption('background', 'w')
    pg.setConfigOption('foreground', 'k')

    self.LeftPanelContent = QtWidgets.QFrame()
    self.LeftPanelContent.setMinimumSize(QtCore.QSize(200, 0))
    self.LeftPanelContent.setMaximumSize(QtCore.QSize(200, 16777215))
    self.LeftPanelContent.setObjectName("LeftPanelContent")     

    self.PanelInfoPatient = QtWidgets.QFrame()
    self.PanelInfoPatient.setMinimumSize(QtCore.QSize(250, 0))
    self.PanelInfoPatient.setMaximumSize(QtCore.QSize(350, 16777215))
    self.PanelInfoPatient.setObjectName("PanelInfoPatient")

    self.PanelRight = QtWidgets.QFrame()
    self.PanelRight.setMinimumSize(QtCore.QSize(700, 0))
    self.PanelRight.setObjectName("PanelRight") 

    self.collaspedLeftPanel = QtWidgets.QFrame()
    self.collaspedLeftPanel.setMinimumSize(QtCore.QSize(75, 0))
    self.collaspedLeftPanel.setMaximumSize(QtCore.QSize(75, 16777215))
    self.collaspedLeftPanel.setObjectName("collaspedLeftPanel")  

    self.leftPanelUi = Ui_LeftPanelContent()
    self.leftPanelUi.setupUi(self.LeftPanelContent)

    self.panelInfoPatientUi = Ui_Form()
    self.panelInfoPatientUi.setupUi(self.PanelInfoPatient)

    self.rightPanelUi = Ui_RightPanel()
    self.rightPanelUi.setupUi(self.PanelRight) 

    self.collaspedLeftPanelUi = Ui_LeftPanelCollapsed()
    self.collaspedLeftPanelUi.setupUi(self.collaspedLeftPanel)

    self.gridLayout_2.addWidget(self.LeftPanelContent, 0, 0, 1, 1)
    self.gridLayout_2.addWidget(self.PanelInfoPatient, 0, 1, 1, 1)
    self.gridLayout_2.addWidget(self.PanelRight,  0, 2, 1, 1)

    self.dialog = QDialog()
    self.dialog.ui = Ui_popupDelete()
    self.dialog.ui.setupUi(self.dialog)
    self.dialog.setWindowTitle("Suppresion patient")

控制人:

class MainWindowController(object):

def __init__(self, appController):
    self.appController = appController
    self.model = None
    self.view = None

def run(self):
    self.view = MainWindow()
    self.view.show()
    self.view.guiAssemblage()

    self.connectSignals()

def createPlot(self):
    self.view.rightPanelUi.ContentRightPanel = pg.GraphicsLayoutWidget();
    self.view.rightPanelUi.verticalLayout.addWidget(self.view.rightPanelUi.ContentRightPanel)
    self.view.rightPanelUi.ContentRightPanel.hide()

def connectSignals(self):
    self.view.leftPanelUi.HideButton.clicked.connect(self.onHideLeftPanelClicked)
    self.view.panelInfoPatientUi.ButtonUpdate.clicked.connect(self.onUpdatePatientClicked)
    self.view.panelInfoPatientUi.ButtonDelete.clicked.connect(self.onDeletePatientClicked)  
    self.view.rightPanelUi.ButtonEvent.clicked.connect(self.onEventClicked);
    self.view.rightPanelUi.ButtonDay.clicked.connect(self.onDayClicked);
    self.view.rightPanelUi.ButtonWeek.clicked.connect(self.onWeekClicked);
    self.view.rightPanelUi.ButtonMonth.clicked.connect(self.onMonthClicked);

我也尝试在guiAssemblage方法中创建GraphicsLayoutWidget,但我有同样的错误 请问我错过了什么?

1 个答案:

答案 0 :(得分:2)

所以我解决了我的问题。

我的pyqtgraph不是最新的。 不要使用PyPy包,它不是最新的。 我升级numpy然后下载并用GitHub源代替我的pyqtgraph模块。

我还从计算机中删除了PyQt5 / PyQt4和PySide,然后重新安装PyQt5

我尝试了几个解决方案,直到这个堆栈:

C:\Users\Nicolas\Desktop\GitRepository\e-servicing\src (master)
λ pip install --upgrade pyqtgraph
Requirement already up-to-date: pyqtgraph in c:\python34\lib\site-packages
Requirement already up-to-date: numpy in c:\python34\lib\site-packages (from pyqtgraph)

C:\Users\Nicolas\Desktop\GitRepository\e-servicing\src (master)
λ python launcher.py
Traceback (most recent call last):
 File "C:\Python34\lib\site-packages\pyqtgraph\Qt.py", line 25, in <module>
import PyQt4
ImportError: No module named 'PyQt4'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Python34\lib\site-packages\pyqtgraph\Qt.py", line 29, in <module>
import PySide
ImportError: No module named 'PySide'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "launcher.py", line 9, in <module>
    import pyqtgraph as pg;
  File "C:\Python34\lib\site-packages\pyqtgraph\__init__.py", line 13, in <module>
from .Qt import QtGui
 File "C:\Python34\lib\site-packages\pyqtgraph\Qt.py", line 32, in <module>
raise Exception("PyQtGraph requires either PyQt4 or PySide; neither package could be imported.")
Exception: PyQtGraph requires either PyQt4 or PySide; neither package could be imported.

从github获取PyQtGraph源代码 它奏效了。