Manipulating QML BarSeries graph values from C++



  • In QML, I have a HorizontalStackedBarSeries that looks like this:

            ChartView {
                title: ""
                width: ioSectionGraphWidth
                height: ioSectionGraphHeight
                legend.visible: true
                legend.font.pointSize: 15
                theme: ChartView.ChartThemeDark
                legend.alignment: Qt.AlignRight
                antialiasing: true
    
                HorizontalStackedBarSeries {
                    id: ioBarchart
                    objectName: "ioBarchart"
                    axisY: BarCategoryAxis { categories: ["INPUT", "OUTPUT"] }
                    BarSet { label: "G Power"; values: [2500, 0] }
                    BarSet { label: "S Power"; values: [550, 0] }
                    BarSet { label: "H Power"; values: [700, 0] }
                    BarSet { label: "Total Output"; values: [0, 1500] }
                }
            }
    

    Is it possible to add a new BarSet to the graph or manipulate an existing BarSet on the graph from C++? For example, could I change one of the values in BarSet { label: "S Power"; values: [550, 0] } to be BarSet { label: "S Power"; values: [123, 0] } from C++?

    I know how to connect signals and slots and how to manipulate basic items like a Label from C++. For example, I was able to change a label in QML from 0 to 190 using C++ when a button is clicked in QML.

    However, I haven't been able to find any resources of the manipulation of QML graphs from C++. How would I go about doing this?

    Thank you in advance.


  • Moderators

    hi @jjgccg

    if you take a look at the BarSet documentation:

    https://doc.qt.io/qt-5/qml-qtcharts-barset.html

    you'll find, that BarSet has a couple of methods that should be useful:

    remove, replace and append in your case.

    You said, you know how to react to a signal from cpp.

    Do that, and give your barsets and ID and you should be good to go :)



  • hey @j-hilk , Thank you for the information.

    I've tried using the replace function in the documentation with QMetaObject::invokeMethod(). First, I gave each of the BarSet's an objectName as you can see here:

    ChartView {
                title: ""
                width: ioSectionGraphWidth
                height: ioSectionGraphHeight
                legend.visible: true
                legend.font.pointSize: 15
                theme: ChartView.ChartThemeDark
                legend.alignment: Qt.AlignRight
                antialiasing: true
    
                HorizontalStackedBarSeries {
                    id: ioBarchart
                    objectName: "ioBarchart"
                    axisY: BarCategoryAxis { categories: ["INPUT", "OUTPUT"] }
                    BarSet { objectName: "gVal"; label: "G Power"; values: [0, 0] }
                    BarSet { objectName: "sVal"; label: "S Power"; values: [550, 0] }
                    BarSet { objectName: "hVal"; label: "H Power"; values: [700, 0] }
                    BarSet { objectName: "totVal"; label: "Total Output"; values: [0, 1500] }
                }
    
    

    Then I got a reference to the gVal BarSet:

    gGraphValues = rootObject->findChild<QObject*>("gVal");
    

    And then I tried invoking the method to change the value 0 to 2500:

        QMetaObject::invokeMethod(gGraphValues, "replace",
                                  Q_ARG(int, 0),
                                  Q_ARG(int, 2500));
    

    Unfortunately this does not work. Do you know what is wrong?


  • Moderators

    @jjgccg
    I thought more along the line of, forwarding the signal to your QML component

    //cpp signal
    void updateGraph(int graphId, int index, int value);
    
    //in qml
    
    Connections{
         target: cppBackend
         onUpdateGraph:{
          if(graphId == 1) 
              graph1.replace(index, value)
          else if(....)
               ....
         }
    }
    


  • @j-hilk I will try this out instead. I edited my reply above after trying the opposite, i.e. changing the value of the graph from C++ instead of changing it from QML after receiving a signal from C++.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.