问题描述
我正在使用Python运行Qt QML应用程序。我正在使用qt图表动态绘制值。为此,我编写了与Qt文档中示波器示例类似的代码,除了使用Python代替C ++。我首先在QML中创建了一个线系列。然后,我使用上下文属性将名为“ Bridge”的类作为“ con”暴露给QML。在名为“ Bridge”的类中,我生成了初始数据。然后,每次计时器计数时,我都会通过将系列传递给“ Bridge”类,然后使用replace函数来更新图表,以便该系列快速获取数据,而不是使用clear和append。
import QtQuick 2.10
import QtQuick.Window 2.5
import QtQuick.Controls 2.4
import QtCharts 2.0
Window {
id: window
title: qsTr("QML and Python graphing dynamically")
width: 640
height: 480
color: "#1b480d"
visible: true
Timer{
id: miTimer
interval: 1 / 24 * 1000 //update every 200ms
running: true
repeat: true
onTriggered: {
con.update_series(chart.series(0))
}
}
Label {
id: label
x: 298
color: "#f1f3f4"
text: qsTr("Graphin dynamically with python")
anchors.horizontalCenterOffset: 0
anchors.top: parent.top
anchors.topMargin: 10
font.bold: true
font.pointSize: 25
anchors.horizontalCenter: parent.horizontalCenter
}
ChartView {
id: chart
x: 180
y: 90
width: 500
height: 300
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
ValueAxis{
id: axisX
min: 0
max: 200
}
ValueAxis{
id: axisY
min: 0
max: 100
}
}
Component.onCompleted: {
console.log("Se ha iniciado QML\n")
var series = chart.createSeries(ChartView.SeriesTypeLine,"My grafico",axisX,axisY)
con.generateData()
}
}
此QML本质上是中间的图表。在Component.onCompleted
中,我创建了一个使用上下文属性类的行系列,并使用python更新了它。
# This Python file uses the following encoding: utf-8
import sys
from os.path import abspath,dirname,join
import random
from pyside2.QtCore import QObject,Slot,QPoint
from pyside2.QtQml import QQmlApplicationEngine
from pyside2.QtCharts import QtCharts
from pyside2.QtWidgets import QApplication # <---
class Bridge(QObject):
def __init__(self,parent=None):
super(Bridge,self).__init__(parent)
self.my_data = []
self.index = -1
@Slot(QtCharts.QAbstractSeries)
def update_series(self,series):
self.index += 1
if(self.index > 4):
self.index = 0
series.replace(self.my_data[self.index])
@Slot()
def generateData(self):
my_data = []
for i in range (5):
my_list = []
for j in range(200):
my_list.append(QPoint(j,random.uniform(0,100)))
self.my_data.append(my_list)
if __name__ == "__main__":
app = QApplication(sys.argv)
engine = QQmlApplicationEngine()
bridge = Bridge()
# Expose the Python object to QML
context = engine.rootContext()
context.setContextProperty("con",bridge)
#engine.load(os.path.join(os.path.dirname(__file__),"main.qml"))
# Get the path of the current directory,and then add the name
# of the QML file,to load it.
qmlFile = join(dirname(__file__),'main.qml')
engine.load(abspath(qmlFile))
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec_())
效果很好。
问题在于树莓派3中无法使用完全相同的程序。错误位于series.replace(self.my_data[self.index])
显示TypeError:
replace(double,double,double) needs 4 argument(s),1 given!
要在树莓派上运行代码,我从以下位置安装了pyside2
库:
https://forum.qt.io/topic/112813/installing-pyside2-on-raspberry-pi/7
其版本为pyside2 5.11.2
。
对于QML模块,我使用了sudo apt-get install qml-module-xxxxxxx
每个需要的库
解决方法
一种可能的解决方案是替换
series.replace(self.my_data[self.index])
具有:
series.clear()
for p in self.my_data[self.index]:
series.append(p.x(),p.y())