Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Manipulating QML BarSeries graph values from C++

Manipulating QML BarSeries graph values from C++

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 2 Posters 701 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • jjgccgJ Offline
    jjgccgJ Offline
    jjgccg
    wrote on last edited by
    #1

    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.

    J.HilkJ 1 Reply Last reply
    0
    • jjgccgJ jjgccg

      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.

      J.HilkJ Online
      J.HilkJ Online
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      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 :)


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      jjgccgJ 1 Reply Last reply
      0
      • J.HilkJ J.Hilk

        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 :)

        jjgccgJ Offline
        jjgccgJ Offline
        jjgccg
        wrote on last edited by jjgccg
        #3

        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?

        J.HilkJ 1 Reply Last reply
        0
        • jjgccgJ jjgccg

          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?

          J.HilkJ Online
          J.HilkJ Online
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @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(....)
                     ....
               }
          }
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          jjgccgJ 1 Reply Last reply
          0
          • J.HilkJ J.Hilk

            @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(....)
                       ....
                 }
            }
            
            jjgccgJ Offline
            jjgccgJ Offline
            jjgccg
            wrote on last edited by
            #5

            @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++.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved