In my C++ class I have
struct trackPoint {
QString lat;
QString lon;
QString elevation;
};
QVector<trackPoint> trackPoints;
In QML I want to access this as a multi-dimensional array of lon,lat pairs
[[0,1],[1,1],[2,1]]
Is this possible using the Q_Property mechanism? As I am pretty sure that structs cannot be exposed to QML?
I've tied:-
Q_PROPERTY(QVector<trackPoint> trackPoints READ gpx)
With a method:-
QVector<trackPoint> GPXFileIO::gpx() const {
return trackPoints;
}
But this gives me the error:-
QMetaProperty::read: Unable to handle unregistered datatype 'QVector<trackPoint>' for property 'GPXFileIO::trackPoints'
A simple way to expose a struct to QML is using Q_GADGET with Q_PROPERTY so we can get each element of the structure, they will not be part of an array. On the other hand QVector is supporting a number of elements with QString, int, QUrl, etc. but not for new types, in which case QVariantList should be used.
main.cpp
main.qml
Output: