将Pyqtgraph嵌入PySide2

时间:2018-09-27 11:41:18

标签: python pyqtgraph pyside2

我想在PySide2应用程序中实现PyQtGraph PlotWidget。使用PyQt5,一切正常。使用PySide2时,我得到的错误显示在底部。我已经发现,有一些工作正在进行中,但是似乎有些人设法使它工作了。但是,我还不能。我正在使用Pyqtgraph 0.10,而不是开发人员分支。我可以改变吗?我该怎么办?

from PySide2.QtWidgets import QApplication, QMainWindow, QGraphicsView, QVBoxLayout, QWidget
import sys
import pyqtgraph as pg


class WdgPlot(QWidget):
    def __init__(self, parent=None):
        super(WdgPlot, self).__init__(parent)
        self.layout = QVBoxLayout(self)

        self.pw = pg.PlotWidget(self)
        self.pw.plot([1,2,3,4])
        self.pw.show()
        self.layout.addWidget(self.pw)
        self.setLayout(self.layout)
if __name__ == '__main__':

    app = QApplication(sys.argv)
    w = WdgPlot()
    w.show()
    sys.exit(app.exec_())

错误:

 QtGui.QGraphicsView.__init__(self, parent)
TypeError: arguments did not match any overloaded call:
  QGraphicsView(parent: QWidget = None): argument 1 has unexpected type 'WdgPlot'
  QGraphicsView(QGraphicsScene, parent: QWidget = None): argument 1 has unexpected type 'WdgPlot'
Traceback (most recent call last):

2 个答案:

答案 0 :(得分:3)

在pyqtgraph的稳定分支中,甚至不支持PySide2,因此它正在导入QtGui.QGraphicsView,它必须属于PyQt4或PySide,因为在PyQt5和PySide2中,QGraphicsView属于子模块QtWidgets而不是QtGui。

在develop分支中,正在实现对PySide2的支持,因此,如果要使用PySide2,则必须使用以下命令手动安装(必须先卸载已安装的pyqtgraph):

git clone -b develop git@github.com:pyqtgraph/pyqtgraph.git
sudo python setup.py install

然后您可以使用:

from PySide2 import QtWidgets
import pyqtgraph as pg


class WdgPlot(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(WdgPlot, self).__init__(parent)
        layout = QtWidgets.QVBoxLayout(self)

        pw = pg.PlotWidget()
        pw.plot([1,2,3,4])
        layout.addWidget(pw)


if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = WdgPlot()
    w.show()
    sys.exit(app.exec_())

enter image description here

更多信息:

答案 1 :(得分:0)

对于任何其他使用Point出错的人-我从'PySide2.QtCore import QPoint'手动导入到文件中,这给我一个错误,然后将Point更改为QPoint。尚无任何官方软件,但目前已修复。

在此处https://github.com/pyqtgraph/pyqtgraph/pull/818/files

查看修订