我正在尝试在 qml 生成的窗口内绘制图表。因为我需要将 QML 用于 GUI(客户端要求)。我设法在 QML 中获取图形窗口,但它什么也没画。我不知道我的代码是否正确,请建议,指出我正确的方向。
主要.py:
#! /usr/bin/python
import os
import sys
from PySide import QtGui 
from PySide import QtCore
from PySide import QtDeclarative
import numpy as np
import pyqtgraph as pg
class Graph (QtDeclarative.QDeclarativeItem):
    def __init__(self, parent = None):
        QtDeclarative.QDeclarativeItem.__init__(self, parent)
        #----------- self.setFlag(QtGui.QGraphicsItem.ItemHasNoContents, False )
        self.dataPlot = np.cos(np.linspace(0, 5 *np.pi , 1000))
        self.graph = pg.PlotItem()
        self.graph.plot( self.dataPlot, pen=(0,255,0))
        self.graph.showGrid(x=True, y=True)
        self.view = pg.GraphicsView()
        self.view.setCentralItem(self.graph)
        #-------------------------------------- self.setCentralWidget(self.view)
        mProxy = QtGui.QGraphicsProxyWidget(self)
        mProxy.setWidget(self.view)
if __name__ == '__main__':    
    app = QtGui.QApplication(sys.argv)
    QtDeclarative.qmlRegisterType(Graph, 'myPyQtGraph', 1, 0, 'PyQtGraph')
    view = QtDeclarative.QDeclarativeView()    
    view.setSource(QtCore.QUrl('main.qml'))
    view.setResizeMode(QtDeclarative.QDeclarativeView.SizeRootObjectToView)
    rootObject = view.rootObject() 
    view.connect(view.engine() , QtCore.SIGNAL('quit()') ,app.instance( ) , QtCore.SLOT('quit()') )
    view.show()    
    sys.exit(app.exec_())
main.qml:
import QtQuick 1.1
import myPyQtGraph 1.0
Rectangle {
    id : page
    width: 900
    height: 400
    color:  "#343434"
    PyQtGraph {
        id: angleGraphID
        anchors{
            top: parent.top
            left: parent.left
            topMargin: 50
            leftMargin: 50
        }
        width: 800
        height: 300
        //color: "#f5deb3"
    }
    Text {
        id: text_Heading
        anchors{
            top: parent.top
            left: parent.left
            topMargin: 20
            leftMargin: 50
        }
        text: qsTr("PyqtGraph QML Test")
        font.pixelSize: 12
    }
}
这就是我现在得到的:
