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.