使用 python 和 QML Oscilloscope 动态绘图效果很好,但相同的程序在 raspberry pi 中不起作用,替换功能不起作用

Graphing dynamically with python and QML Oscilloscope works great, but the same program doesn't work in raspberry pi, replace function don't work

我正在 运行 使用 Python 创建一个 Qt QML 应用程序。我正在使用 qt 图表动态绘制值。为此,我编写了与 Qt 文档中的示波器示例类似的代码,除了我使用 Python 而不是 C++。我首先在 QML 中创建了一个线系列。然后我使用上下文 属性 向 QML 公开了一个名为“Bridge”的 class 作为“con”。在名为“Bridge”的 class 中,我生成了初始数据。然后,每次计时器计数时,我都会更新图表,方法是将系列传递给“Bridge”class,然后使用替换函数,以便系列快速获取数据,而不是使用清除和追加。

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 中,我创建了一个使用上下文 属性 class 的线系列,我使用 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_())

效果很好。

问题是完全相同的程序在 raspberry pi 中不起作用 3. 错误在 series.replace(self.my_data[self.index])

它说类型错误:

replace(double,double,double,double) needs 4 argument(s), 1 given!

为了 运行 raspberry pi 上的代码,我从以下位置安装了 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())