How can I change all the points in an XYSeries in qml or PySide2?

前端 未结 2 1241
忘掉有多难
忘掉有多难 2021-01-23 18:51

I\'m kinda new to PySide2 and QML and I really need a way to replace all the points in an XYSeries at once. Since the QML item does not have a function that does so, I thought I

2条回答
  •  深忆病人
    2021-01-23 19:53

    One possible solution is to create a class that allows access to a QML object from Python, in this case I create the helper class that I export to QML through setContextProperty by linking the series with a qproperty.

    main.py

    import random
    from PySide2 import QtCore, QtWidgets, QtQml
    from PySide2.QtCharts import QtCharts
    
    class Helper(QtCore.QObject):
        serieChanged = QtCore.Signal()
    
        def __init__(self, parent=None):
            super(Helper, self).__init__(parent)
            self._serie = None
    
        def serie(self):
            return self._serie
    
        def setSerie(self, serie):
            if self._serie == serie:
                return
            self._serie = serie
            self.serieChanged.emit()
    
        serie = QtCore.Property(QtCharts.QXYSeries, fget=serie, fset=setSerie, notify=serieChanged)
    
        @QtCore.Slot(list)
        def replace_points(self, points):
            if self._serie is not None:
                self._serie.replace(points)
    
    class Provider(QtCore.QObject):
        pointsChanged = QtCore.Signal(list)
    
        def __init__(self, parent=None):
            super(Provider, self).__init__(parent)
            timer = QtCore.QTimer(
                self, 
                interval=100,
                timeout=self.generate_points
            )
            timer.start()
    
        @QtCore.Slot()
        def generate_points(self):
            points = []
            for i in range(101):
                point = QtCore.QPointF(i, random.uniform(-10, 10))
                points.append(point)
            self.pointsChanged.emit(points)
    
    if __name__ == '__main__':
        import os
        import sys
        app = QtWidgets.QApplication(sys.argv)
        helper = Helper()
        provider = Provider()
        provider.pointsChanged.connect(helper.replace_points)
        engine = QtQml.QQmlApplicationEngine()
        engine.rootContext().setContextProperty("helper", helper)
        file = os.path.join(os.path.dirname(os.path.realpath(__file__)), "main.qml")
        engine.load(QtCore.QUrl.fromLocalFile(file))
        if not engine.rootObjects():
            sys.exit(-1)
        sys.exit(app.exec_())
    

    main.qml

    import QtQuick 2.9
    import QtQuick.Window 2.2
    import QtCharts 2.3
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
        ChartView{
            anchors.fill: parent
            LineSeries{
                id: serie
                axisX: axisX
                axisY: axisY
            }
            ValueAxis {
                id: axisX
                min: 0
                max: 100
            }
    
            ValueAxis {
                id: axisY
                min: -10
                max: 10
            }
            Component.onCompleted: helper.serie = serie
        }
    }
    

提交回复
热议问题