2
votes

Here is my QML code.

Item {
    id: root
    width: 1200
    height: 800
    ChartView {
        id: chartView
        objectName: "chartView"
        width: 400
        height: 200
        theme: ChartView.ChartThemeDark
        antialiasing: true
        opacity: 0.6

        LineSeries {
            id: lineSeries
            objectName: "lineSeries"
            name: "Fertilizer Consumption"
            XYPoint { x: 0; y: 20 }
            XYPoint { x: 1.1; y: 18 }
            XYPoint { x: 4.1; y: adapter.fertilizer_consumption }
            XYPoint { x: 8.0; y: 17 }
            XYPoint { x: 9.0; y: 16 }
       }
   }
}

and here is my C++ code in the Adapter class that specifies the fertilizer_consumption property

class Adapter : public QObject
{
    Q_OBJECT
public:
    explicit Adapter(QObject *parent = 0);

    Q_PROPERTY(int fertilizer_consumption READ getFertilizerConsumption WRITE setFertilizerConsumption NOTIFY fertilizerConsumptionChanged)
    int getFertilizerConsumption() const;
    void setFertilizerConsumption(int fertilizer_consumption);

signals:
    void fertilizerConsumptionChanged();

public slots:
    void updateFertilizerConsumption(int fertilizer_consumption);

private:
    int m_fertilizer_consumption;
}

I have set the adapter object as context property in the begging of my application

int main(int argc, char *argv[]) {

    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QApplication app(argc, argv);

    // create view window
    Adapter adapter;
    QQuickView view;
    view.rootContext()->setContextProperty("adapter", &adapter);
    view.setSource(QUrl("qrc:/Main.qml"));
    view.setResizeMode(QQuickView::SizeRootObjectToView);
    view.resize(1200, 800);

    QObject::connect(view.engine(), SIGNAL(quit()), &view, SLOT(close()));
    view.show();

    return app.exec();
}

I want to update a XYPoint of the LineSeries item dynamically from the exposed property using the SIGNAL/SLOT logic.

But when i try to update the XYPoint through the property with this function, nothing happens

void Adapter::updateFertilizerConsumption(int fertilizer_consumption) {
    m_fertilizer_consumption = fertilizer_consumption;
    emit fertilizerConsumptionChanged();
}

The only successful update was in the time of creation of the adapter object

Adapter::Adapter(QObject *parent) : QObject(parent) {
    m_fertilizer_consumption = 60;
}

Any help would be appreciated. Thanks.

1
I spend hours to get this working. I could not solve. I only find one who wrote a function that clears and re-fill chart. - Muhammet Ali Asan
Thanks for your time. Is really a strange problem. I don't mentioned that, but to be able to compile the application using the LineSeries item, I had to create QApplication and not a QGuiApplication. This means probably than the LineSeries is a qwidget based item... I don't know if that restricts you in some way. - dakridas

1 Answers

1
votes

I think your solution lies in the qml oscilloscope example. https://doc.qt.io/qt-5/qtcharts-qmloscilloscope-example.html

There what you have to do is, to change the series value, either with append or in your case with replace methods. It should update your chart automatically. You could try to take the datasource and ScopeView part of the example above, where the ScopeView basically updates its series by passing them to the datasource, through update(QAbstractSeries*) method. The datasource is the backend of the chart and has knowledge of the data history and whenever the ScopeView calls the update(QAbstractSeries*) it replaces the series with the content of m_data. What you need to add on your own is a function that updates the datasource m_data member with your desired values.

Hope this was helpful.