how to transferred QLineSeries from c++ to qml
Unsolved
QML and Qt Quick
-
version 6.8
I pass the c++ QLinSeries into qml through signals and slots,Slot functions trigger normally,but the lineSeries parameter of the qml addSeries function is empty; it has no properties.I read this document.Once a class is registered as a QML type, it can be used as a data type for exchanging data between QML and C++. Isn't QLineSeries already registered with the qml type? Its type in qml is LineSeries.
cpp
#pragma once #include <QObject> #include <QLineSeries> class MainWindowViewModel : public QObject { Q_OBJECT public: void addSeries(QLineSeries *data); signals: void seriesAdded(QLineSeries *data); };
main.cpp
QList<QPointF> dataList; ... QLineSeries *lineSeries = new QLineSeries(); lineSeries->append(dataList); lineSeries->setColor(QColor("black")); QQmlApplicationEngine engine; MainWindowViewModel *viewModel = new MainWindowViewModel(); engine.loadFromModule("chromChart", "MainWindow"); QObject::connect(viewModel, SIGNAL(seriesAdded(QLineSeries *)), engine.rootObjects()[0], SLOT(addSeries(QLineSeries *))); //emit signals viewModel->addSeries(lineSeries);
qml
function addSeries(lineSeries: LineSeries) { try { console.log("trigger slot function") graphsView.addSeries(lineSeries); for (var prop in lineSeries) { console.log("Object item:", prop, "=", lineSeries[prop]); } } catch (e) { console.log(e); } }