首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在QML生成的窗口中未绘制图形(pyqtgraph)

在QML生成的窗口中未绘制图形(pyqtgraph)
EN

Stack Overflow用户
提问于 2012-11-06 04:44:26
回答 1查看 1.9K关注 0票数 3

我正在尝试在qml生成的窗口中绘制图形。因为我需要将QML用于GUI (客户端要求)。我设法在QML中得到图形窗口,但它什么也没画出来。我不知道我的代码是否正确,请给我建议,给我指出正确的方向。

main.py:

代码语言:javascript
复制
#! /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:

代码语言:javascript
复制
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
    }
}

这就是我现在得到的:

EN

回答 1

Stack Overflow用户

发布于 2012-11-06 06:13:14

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

然而,它只在QML上下文中出现问题,因为PySide在QML之外工作得很好。你可以通过在pyqtgraph/graphicsItems/GraphicsObject.py中注释掉'itemChange‘方法来解决这个问题。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13240066

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档