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. Changing the properties of the chart from Qt Widget Application
Qt 6.11 is out! See what's new in the release blog

Changing the properties of the chart from Qt Widget Application

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 4 Posters 1.4k Views 3 Watching
  • 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.
  • P Offline
    P Offline
    pirates21
    wrote on last edited by pirates21
    #1

    Hello

    In my "Qt Widget Application I load a QML object, which is a graph:

    ChartView {
    title: "Line"
    anchors.fill: parent
    antialiasing: true

    LineSeries {
        name: "LineSeries"
        XYPoint { x: 0; y: 0 }
        XYPoint { x: 1.1; y: 2.1 }
        XYPoint { x: 1.9; y: 3.3 }
        XYPoint { x: 2.1; y: 2.1 }
        XYPoint { x: 2.9; y: 4.9 }
        XYPoint { x: 3.4; y: 3.0 }
        XYPoint { x: 4.1; y: 3.3 }
    }
    

    }

    I would like to change the values of the "LineSeries" chart from my application.

    In other QML objects, I do it in this way:
    QQmlProperty(ui->quickWidget->rootObject(),"currentValue").write(variable);

    How do you change the "LineSeries" values in the same way?

    Best Regards

    raven-worxR 1 Reply Last reply
    0
    • oria66O Offline
      oria66O Offline
      oria66
      wrote on last edited by
      #2

      Hi @pirates21. In order to send data to qml components in a Qt Widget Application, you must obtain the QQuickItem* pointer, then you can use two ways (it's possible that exists others but I used these two):

      1. You can use the SIGNAL/SLOT mechanism.
      2. You can use the QMetaObject::invokeMethod(...).

      Please refer to http://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html.

      Good luck.

      The truth is out there

      1 Reply Last reply
      0
      • P Offline
        P Offline
        pirates21
        wrote on last edited by pirates21
        #3

        @oria66 : thank you for your answer. Could you give an example that would refer to lineseries in QML?

        1 Reply Last reply
        0
        • P pirates21

          Hello

          In my "Qt Widget Application I load a QML object, which is a graph:

          ChartView {
          title: "Line"
          anchors.fill: parent
          antialiasing: true

          LineSeries {
              name: "LineSeries"
              XYPoint { x: 0; y: 0 }
              XYPoint { x: 1.1; y: 2.1 }
              XYPoint { x: 1.9; y: 3.3 }
              XYPoint { x: 2.1; y: 2.1 }
              XYPoint { x: 2.9; y: 4.9 }
              XYPoint { x: 3.4; y: 3.0 }
              XYPoint { x: 4.1; y: 3.3 }
          }
          

          }

          I would like to change the values of the "LineSeries" chart from my application.

          In other QML objects, I do it in this way:
          QQmlProperty(ui->quickWidget->rootObject(),"currentValue").write(variable);

          How do you change the "LineSeries" values in the same way?

          Best Regards

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by raven-worx
          #4

          @pirates21
          as the docs state LineSeries instantiates QLineSeries.
          So you need a reference to your LineSeries object.

          QML:

          ChartView {
              title: "Line"
              anchors.fill: parent
              antialiasing: true
          
             property alias  lineSeries: series // <<<<<<<<<<
          
              LineSeries {
                  id: series
                  ....
              }
          }
          

          Then in C++:

          QObject* lineSeriesObj = QQmlProperty(ui->quickWidget->rootObject(),"lineSeries").read().value<QObject*>();
          QLineSeries* lineSeries = qobject_cast<QLineSeries*>(lineSeriesObj);
              lineSeries->clear();
              lineSeries->append( ... );
          

          Note that i've haven't tested this code. I don't know if thats enough to also let the chart view also update itself.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          1
          • P Offline
            P Offline
            pirates21
            wrote on last edited by
            #5

            @raven-worx: You are Genius! Thank You very much!

            1 Reply Last reply
            0
            • GrecKoG Offline
              GrecKoG Offline
              GrecKo
              Qt Champions 2018
              wrote on last edited by GrecKo
              #6

              What @raven-worx showed is not recommended (cf. http://doc.qt.io/qt-5/qtqml-cppintegration-overview.html#interacting-with-qml-objects-from-c & https://youtu.be/vzs5VPTf4QQ?t=23m20s & https://doc.qt.io/qt-5/qtquick-bestpractices.html#interacting-with-qml-from-c)

              Accessing a QML object from C++ add unnecessary coupling. Your c++ business layer should merely expose data that is then used by the QML layer.

              Here what you could do instead is expose a c++ QAbstractListModel, and use that in QML with a VXYModelMapper.

              Alternatively, you could pass your line series to your c++ layer via a slot (or invokable function) and it will then populate it. Your c++ code will still modify a qml object, but at least it didn't have to retrieve it by itself. It's less than ideal but still better.

              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