图形(pyqtgraph)没有被绘制在QML生成的窗口内

时间:2012-11-05 20:44:26

标签: python qt pyqt pyside pyqtgraph

我正在尝试在qml生成的窗口中绘制图形。因为我需要使用QML进行GUI(客户端要求)。我设法在QML中获取图形窗口,但它没有绘制任何内容。我不知道我的代码是否正确,请建议,指出我正确的方向。

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

这就是我现在所得到的: enter image description here

1 个答案:

答案 0 :(得分:2)

我最好的猜测是问题与此错误有关: http://srinikom.github.com/pyside-bz-archive/932.html

然而,它似乎只是QML上下文中的问题,因为PySide在QML之外工作正常。 你可以通过在pyqtgraph / graphicsItems / GraphicsObject.py中注释掉'itemChange'方法来解决这个问题。