title: Connect to QML Chart from c++
body: <p>I have a c++ class which collects data (currently just random numbers generated between upper and lower thresholds) and packages it into a QVector (QPointF). I have a QML file which includes a ChartView. I'd like to plot the collected c++ data in my QML ChartView (LineSeries), and update the plot each time the data collection function in c++ finishes. I'd like to send the collected QVector to a secondary c++ class to perform the QML ChartView update.</p>
  
  <p>Below are excerpts of the various pertinent files.</p>
  
  <p>data.h:</p>
  
  <pre><code>#ifndef DATA_H
  #define DATA_H
  
  #include &lt;QObject&gt;
  #include &lt;QTimer&gt;
  #include &lt;QtCore/QObject&gt;
  
  #include &lt;QAbstractSeries&gt;
  #include &lt;QXYSeries&gt;
  
  class Data : public QObject
  {
      Q_OBJECT
      Q_PROPERTY(QVector&lt;int&gt; wVector READ wVector NOTIFY wVectorChanged)
  
  public:
      Data();
  
      Q_PROPERTY(float wValue READ wValue NOTIFY wValueChanged)
      float wValue(){return this-&gt;m_wValue;}
  
      QVector&lt;int&gt; wVector(){return m_wVector;}
  
  signals:
      void wValueChanged();
      void wVectorChanged();
  
  private slots:
      void wTimeout();
  
  public slots:
  
  private:
      float    m_wValue;
      QTimer * m_wTimer;
  
      QVector&lt;int&gt; m_wVector;
      QList&lt;QVector&lt;QPointF&gt; &gt; m_data;
  
  };
  
  #endif // DATAD_H
  </code></pre>
  
  <p>data.cpp:</p>
  
  <pre><code>#include "data.h"
  #include "chart.h"
  #include &lt;QDebug&gt;
  #include &lt;iostream&gt;
  #include &lt;QTimer&gt;
  #include &lt;QtCharts/QXYSeries&gt;
  #include &lt;QtCharts/QAreaSeries&gt;
  #include &lt;QtQuick/QQuickView&gt;
  #include &lt;QtQuick/QQuickItem&gt;
  #include &lt;QtMath&gt;
  
  Data::Data()
  
  {
      this-&gt;m_wTimer = new QTimer(this);
      this-&gt;m_wTimer-&gt;setInterval((1000 / 5));
      connect(this-&gt;m_wTimer, &amp;QTimer::timeout, this, &amp;Data::wTimeout);
      this-&gt;m_wTimer-&gt;start();
  }
  
  void Data::wTimeout()
  {
      int HIGH = 100;
      int LOW = 0;
      this-&gt;m_wValue = rand() % (HIGH - LOW + 1) + LOW;
  
      if (m_wVector.size() &gt;= 5 &amp;&amp; !m_wVector.isEmpty())
          m_wVector.removeFirst();
  
      this-&gt;m_wVector.append(m_wValue);
      m_data.clear();
  
      QVector&lt;QPointF&gt; dataStore;
  
      for (int i(0); i &lt; m_wVector.size(); i++) {
          dataStore.append(QPointF(i+1, m_wVector[i]));
      }
  
      m_data.append(dataStore);
  
      emit wValueChanged();
      //SEND m_data to Chart.update() here??
  }
  </code></pre>
  
  <p>chart.h:</p>
  
  <pre><code>#ifndef CHART_H
  #define CHART_H
  
  #include &lt;QtCore/QObject&gt;
  #include &lt;QtCharts/QAbstractSeries&gt;
  #include &lt;QXYSeries&gt;
  class QTimer;
  
  QT_CHARTS_USE_NAMESPACE
  
  class Chart : public QObject
  {
      Q_OBJECT
  public:
      ConfigurationChart();
  
      Q_INVOKABLE void setSeries(QAbstractSeries *series);
  
  public slots:
      void update();
  
  private:
      QList&lt;QVector&lt;QPointF&gt; &gt; m_data;
      int m_index;
      QXYSeries *mSeries;
      QTimer *timer;
  };
  
  #endif // CHART_H
  </code></pre>
  
  <p>chart.cpp:</p>
  
  <pre><code>#include "chart.h"
  #include &lt;QtCharts/QXYSeries&gt;
  #include &lt;QtCharts/QAreaSeries&gt;
  #include &lt;QtQuick/QQuickView&gt;
  #include &lt;QtQuick/QQuickItem&gt;
  #include &lt;QtCore/QDebug&gt;
  #include &lt;QtCore/QtMath&gt;
  #include &lt;QTimer&gt;
  
  QT_CHARTS_USE_NAMESPACE
  
  Q_DECLARE_METATYPE(QAbstractSeries *)
  Q_DECLARE_METATYPE(QAbstractAxis *)
  
  Chart::Chart()
  {
      qRegisterMetaType&lt;QAbstractSeries*&gt;();
      qRegisterMetaType&lt;QAbstractAxis*&gt;();
  
  }
  
  void Chart::update()//Need to pass the m_data QList&lt;QVector&lt;QPointF&gt;&gt; parameter here??
  {
      if (mSeries) {
          m_index++;
          if (m_index &gt; m_data.count() - 1)
              m_index = 0;
  
          QVector&lt;QPointF&gt; points = m_data.at(m_index);
          mSeries-&gt;replace(points);
      }
  }
  
  void Chart::setSeries(QAbstractSeries *series)
  {
      //I don't know how this is called, nor how this actually establishes a link to the QML chart??
      if (series) {
          mSeries = static_cast&lt;QXYSeries *&gt;(series);
      }
  }
  </code></pre>
  
  <p>main.cpp:</p>
  
  <pre><code>#include &lt;QApplication&gt;
  #include &lt;QQmlApplicationEngine&gt;
  #include &lt;QQmlEngine&gt;
  #include &lt;QQmlContext&gt;
  #include &lt;QQuickItem&gt;
  #include &lt;thread&gt;
  #include &lt;chrono&gt;
  #include &lt;QDebug&gt;
  #include &lt;time.h&gt;
  #include &lt;iostream&gt;
  #include &lt;typeinfo&gt;
  #include &lt;QtCharts&gt;
  #include &lt;QtQuick/QQuickView&gt;
  
  #include "data.h"
  #include "hart.h"
  
  using namespace std;
  using namespace QtCharts;
  
  int main(int argc, char *argv[])
  {
      QApplication app(argc, argv);
  
      QQmlApplicationEngine engine;
  
      engine.rootContext()-&gt;setContextProperty(QStringLiteral("Data"), new Data());
      engine.rootContext()-&gt;setContextProperty(QStringLiteral("Chart"), new Chart());
  
      engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
      if (engine.rootObjects().isEmpty())
          return -1;
  
      return app.exec();
  }
  </code></pre>
  
  <p>main.qml:</p>
  
  <pre><code>import QtQuick 2.8
  ...
  import QtCharts 2.2
  
  //I've removed unnecessary QML elements for simplicity
  
  ChartView {
      id: chartView
      width: parent.width
      height: parent.height
      anchors.fill: parent
      margins.bottom: 0
      margins.top: 0
      margins.left: 0
      margins.right: 0
      animationOptions: ChartView.NoAnimation
      antialiasing: true
      legend.visible: false
      backgroundColor: "#1f1f1f"
  
      ValueAxis {
          id: axisY1
          min: 0
          max: 100
          gridVisible: false
          color: "#ffffff"
          labelsColor: "#ffffff"
          labelFormat: "%.0f"
      }
  
      ValueAxis {
          id: axisX
          min: 0
          max: 50
          gridVisible: false
          color: "#ffffff"
          labelsColor: "#ffffff"
          labelFormat: "%.0f"
          tickCount: 5
      }
  
      LineSeries {
          id: lineSeries1
          name: "signal 1"
          color: "white"
          axisX: axisX
          axisY: axisY1
      }
  }
  </code></pre>
  
  <p>My question: how do I connect chart.cpp to QML LineSeries to plot the chart.cpp data (received from data.cpp) in the QML ChartView?</p>
  
  <p>I'm using Qt5.9.2.</p>