我正在尝试在QML桌面应用程序内部使用pyqtgraph。
我正在使用python 3.7(pyqtgraph的当前dev分支)和pyside2(qt 5.11.2)。
这是我的example.qml
import QtQuick 2.11
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.11
import my_pyqtgraph 1.0
ApplicationWindow {
visible: true
width: 800
height: 600
Rectangle {
id: plotRect
color: "white"
Layout.fillWidth: true
Layout.fillHeight: true
PyQtGraph {
id: waveformPlot
width: 700
height: 500
}
}
}
这是我的example.py
import sys
import os
import numpy as np
import pyqtgraph as pg
os.environ["QT_API"] = "pyside2"
from qtpy.QtQuick import QQuickItem
from qtpy.QtCore import QUrl
from qtpy.QtWidgets import QApplication, QGraphicsProxyWidget
from qtpy.QtQml import qmlRegisterType, QQmlApplicationEngine
class Graph(QQuickItem):
def __init__(self, parent=None):
super().__init__(parent)
self.dataPlot = np.cos(np.linspace(0, 2 * np.pi, 1000))
self.graph = pg.PlotItem()
self.graph.plot(self.dataPlot, pen=(0, 255, 0))
self.view = pg.GraphicsView()
self.view.setCentralItem(self.graph)
mproxy = QGraphicsProxyWidget(self.graph)
mproxy.setWidget(self.view)
if __name__ == "__main__":
app = QApplication(sys.argv)
engine = QQmlApplicationEngine()
qmlRegisterType(Graph, "my_pyqtgraph", 1, 0, "PyQtGraph")
engine.load(QUrl("fred/example.qml"))
sys.exit(app.exec_())
我最终只有一个指定大小的空白窗口。
我碰到了这篇文章:Graph (pyqtgraph) is not getting plotted inside QML generated window但是那里的答案已经过时了。来自qt5文档:
https://doc.qt.io/qt-5/qtquick-porting-qt5.html#qdeclarativeitem-and-qdeclarativeview
移植到QQuickItem时,请注意QDeclarativeItem继承自QGraphicsItem;相比之下,QQuickItem直接从QObject继承,并且任何特定于QGraphicsItem的功能都不再可用。
任何建议将不胜感激。