python-如何将qml scatterseries添加到现有的qml定义的图表视图中?

2021-01-08 24点热度 0人点赞 0条评论
我尝试使用python将series-on-demand添加到QML中定义的现有ChartView中。我发现了一个例子,展示了如何在C++中做到这一点(摘自 https://doc.qt.io/archives/qt-5.11/qml-qtcharts-chartview.html#createSeries-method ):

// lineSeries is a LineSeries object that has already been added to the ChartView; re-use its axes
var myAxisX = chartView.axisX(lineSeries);
var myAxisY = chartView.axisY(lineSeries);
var scatter = chartView.createSeries(ChartView.SeriesTypeScatter, "scatter series", myAxisX, myAxisY);

但我找不到用Python实现这一点的方法。以下是迄今为止我尝试的一些片段:

qml片段(最初只有1个散点序列):

        ChartView {
            id: bscan0
            ScatterSeries{
                id: hits0
                axisX: ValueAxis {
                    id: bscan0_xAxix
                    min: 0
                    max: 10
                }
                axisY: ValueAxis {
                    id: bscan0_yAxis
                    min: -105
                    max: 1
                }
            }

qml js函数将chartview传递给python,以便它可以添加另一个系列:

dataModel.addChartSeries(bscan0, hits0)

AddChartSeries的python代码段:

    @Slot(QObject, QObject)
    def addChartSeries(self, chartView, chart_series):
        myAxisX = chartView.axisX(chart_series) # reuse the axis from existing series
        myAxisY = chartView.axisY(chart_series) # reuse the axis from existing series

#         This function equivalent to the c++ one doesn't exit
#         myChartSeries = chartView.createSeries(QtCharts.SeriesTypeScatter, "scatter series", myAxisX, myAxisY)

#       So try another way:
        myChartSeries = QtCharts.QScatterSeries()
        myChartSeries.setName("scatter series")
        myChartSeries.attachAxis(myAxisX) 
        myChartSeries.attachAxis(myAxisY)
        myChartSeries.append(5, -10) 
        myChartSeries.append(5, -20) 
        myChartSeries.append(5, -30)

#         Try to get chart from existing series. Doesn't work
#         Error says that chart_series is not in a chart (it is!)
#         myChart = chart_series.chart()
# Series not in the chart. Please addSeries to chart first.

#         Try to get chart from chartview passed across. Doesn't work
#         Error says that object has no .chart attribute (same for .chart and .chart()):
#         myChart = chartView.chart
# Error: 'PySide2.QtQuick.QQuickItem' object has no attribute 'chart'

#         It seems I must add series to chart like this, not to chartView, 
#         but I can't find a way to get the chart for the chartView.
#         myChart.addSeries(myChartSeries) 

上面的python函数在我的类“datamodel”中,我像这样传递给qml(datamodel类对于我用它做的许多其他事情都很好,因此没有问题):

    dataModel = DataModel()    
    self.rootContext().setContextProperty("dataModel", dataModel)

查看隐藏内容需要支付:¥1
查看

未经允许不得转载!python-如何将qml scatterseries添加到现有的qml定义的图表视图中?

本文地址:https://ans.52learn.online/2150

ANS52LEARN

DO BEST