Graph (pyqtgraph) is not getting plotted inside QM

2019-08-03 01:43发布

问题:

I am trying to plot a graph inside a qml generated window. Since i need to use QML for GUI ( client requirement). I manage to get the graph window inside QML, but its plotting nothing. I dont know my code is correct or not, please suggest, point me in the right direction.

main.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
    }
}

this is what i am getting right now:

回答1:

My best guess is that the problem is related to this bug: http://srinikom.github.com/pyside-bz-archive/932.html

However it only appears to be a problem within the QML context, as PySide works fine outside of QML. You can work around the problem by commenting out the 'itemChange' method in pyqtgraph/graphicsItems/GraphicsObject.py.