Can't retroactively add points to ScatterSeries
-
Hi
I recently looked forward to the addition QtGraphs to QML and have been trying to create 2D scatter graph that updates during the runtime of my program. To test this, I simply wanted to see if I could add new points to my ScatterSeries by pressing a button as seen in the following code:
import QtQuick import QtQuick.Controls 6.7 import QtQuick3D 6.7 import QtGraphs 6.7 import QtQuick.Window import QtQuick.Layouts Window { width: 1024 height: 768 visible: true Rectangle { id: rectangle anchors.fill: parent Rectangle { id: rectangle1 x: 520 y: 0 width: 504 height: 768 color: "#000000" GraphsView { id: graphView visible: true anchors.fill: parent theme: GraphTheme { readonly property color c1: "#DBEB00" readonly property color c2: "#373F26" readonly property color c3: Qt.lighter(c2, 1.5) colorTheme: GraphTheme.ColorThemeDark gridMajorBarsColor: c3 gridMinorBarsColor: c2 axisXMajorColor: c3 axisYMajorColor: c3 axisXMinorColor: c2 axisYMinorColor: c2 axisXLabelsColor: c1 axisYLabelsColor: c1 } component Marker: Rectangle { id: marker width: 16 height: 16 color:"#ffffff" radius: width * 0.5 border.width: 4 border.color: "#000000" } ScatterSeries { id: scatterSeries axisX: ValueAxis { max: 5 tickInterval: 1 minorTickCount: 9 labelDecimals: 1 } axisY: ValueAxis { max: 10 tickInterval: 1 minorTickCount: 4 labelDecimals: 1 } // width: 4 // selectable:true // hoverable:true pointMarker: Marker {} XYPoint { x: 0 y: 2 } XYPoint { x: 1 y: 1.2 } XYPoint { x: 2 y: 3.3 } XYPoint { x: 5 y: 2.1 } } } } Button { id: pointSpawn x: 212 y: 312 text: qsTr("Add Point") checked: false checkable: false highlighted: false flat: false Connections { target : pointSpawn onClicked: { scatterSeries.append(5,6) } } } } }
I thought I could this by just appending two real values to the QScatterSeries because it is an Q_invokable function according to the documentation. When I try to run however I get the following error:
TypeError: Property 'append' of object QScatterSeries(0x2354355f5f0) is not a function
I am not sure how to fix this. Am I somehow being connected to the wrong QScatterSeries or am I just doing something wrong in my code. Would really appreciate some advice on this.
Kind regards
Olivier -
I'm still using Qt 5 but I had a look at the doc and it seems to me that the API exposed to QML is more limited in QtGraphs than it was in QtCharts. The
append
method is available in theQXYSeries
C++ implementation class but not in QML as far as I can see. You might need to write a function in some backend C++ code that you expose to QML. This function would accept aQXYSeries*
and you will be able to accessappend
in the function's C++ implementation. You will be able to pass yourscatterSeries
object to this function from QML and it will be received as aQXYSeries*
.