Embed Pyqtgraph to PySide2

╄→尐↘猪︶ㄣ 提交于 2019-11-30 18:11:26

问题


I'd like to implement a PyQtGraph PlotWidget into a PySide2 application. With PyQt5 everything works. With PySide2 I get the Error shown at the bottom. I already found out, that there's some work in progress, but also it seems that a few people managed to get this working. However, I was not able yet. I am using Pyqtgraph 0.10 and not the developer branch. Shall I change? What do I need to do?

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_())

Error:

 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):

回答1:


In the stable branch of pyqtgraph even PySide2 is not supported, so it is importing QtGui.QGraphicsView that must belong to PyQt4 or PySide since in PyQt5 and PySide2 QGraphicsView belongs to the submodule QtWidgets and not to QtGui.

In the develop branch, PySide2 support is being implemented, so if you want to use PySide2 you will have to install it manually using the following commands (you must first uninstall your installed pyqtgraph):

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

Then you can use:

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_())

More information in:

  • https://github.com/pyqtgraph/pyqtgraph/issues/342



回答2:


For anyone else getting errors with Point - I manually imported from 'PySide2.QtCore import QPoint' into the files giving me an error and changed Point to QPoint. Not anything official but fixes it for now.

View fix here https://github.com/pyqtgraph/pyqtgraph/pull/818/files



来源:https://stackoverflow.com/questions/52536194/embed-pyqtgraph-to-pyside2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!