问题描述
我想将 PyQtGraph PlotWidget 实现到 PySide2 应用程序中.使用 PyQt5 一切正常.使用 PySide2 我得到底部显示的错误.我已经发现,有一些工作正在进行中,但似乎也有一些人设法使这项工作正常进行.但是,我还做不到.我使用的是 Pyqtgraph 0.10 而不是开发者分支.我要不要改变?我需要做什么?
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_())
错误:
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):
推荐答案
在pyqtgraph的stable分支中甚至不支持PySide2,所以它导入了必须属于PyQt4或PySide的QtGui.QGraphicsView,因为PyQt5和PySide2属于QGraphicsView到子模块 QtWidgets 而不是 QtGui.
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.
在开发分支中,正在实现 PySide2 支持,因此如果您想使用 PySide2,则必须使用以下命令手动安装它(您必须先卸载已安装的 pyqtgraph):
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 [email protected]: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_())
更多信息:
这篇关于将 Pyqtgraph 嵌入到 PySide2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!