Search code examples
arraysqtqmlqt-quick

How do you declare an array of Qt.points in QML?


In the C++ class I have something like this (I've tried different things):

class X : public QObject  {
    Q_OBJECT

public:
    Q_INVOKABLE void save(int _n, QObject *_points);

I tried this in the qml file, but it dosen't compile:

property var Qt.point points: []

I want to pass this array from QML (in a javascript function) to a C++ function like this:

x.save(root.points.length, root.points)

If I declare points like this:

property var points: []

the C++ function is called, but points is NULL.


Solution

  • property var Qt.point points: []
    

    That makes no sense, the syntax is property + type + name. Qt.point is not a type but a function that returns an object of type point. And a var that contains a JS array most certainly will not be converted to a QObject * in C++.

    I suggest you implement your array of points as a C++ class, with accessor functions to access or manipulate the data from QML. So you will have a QVector<QPoint> in your X class, along with functions to get the size, get or set points, append, prepend, insert and whatnot. So when you call save() you can directly access the C++ data. That would be the most efficient solution.