python - 将 Pyqtgraph 嵌入到 PySide2
问题描述
我想将 PyQtGraph PlotWidget 实现到 PySide2 应用程序中。使用 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):
解决方案
在 pyqtgraph 的稳定分支中,甚至不支持 PySide2,因此它正在导入必须属于 PyQt4 或 PySide 的 QtGui.QGraphicsView,因为在 PyQt5 和 PySide2 中 QGraphicsView 属于子模块 QtWidgets 而不是 QtGui。
在开发分支中,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_())
更多信息在:
推荐阅读
- python - 为什么我没有在其派生类的实例中初始化继承类实例属性失败时收到错误消息?
- node.js - Plesk Git 其他部署操作 npm 未执行
- ssl - Helm - 错误:x509:无法验证 127.0.0.1 的证书,因为它不包含任何 IP SAN
- go - 为什么我们需要在 goroutine 中运行无缓冲通道,但缓冲通道可以在没有 goroutine 的情况下工作?
- windows - 等将 www.example.com 托管到本地主机?
- python - Python 索引必须是整数
- python-3.x - 是否可以将浮点数转换为双精度数?
- javascript - 使用外部链接将图像上传到strapi
- reactjs - 有没有办法替换从子组件传递给 ReactDOM.render() 的第一个参数的内容?
- android - Mockito 不模拟存储库