你如何在 QML 中声明一个 Qt.points 的数组?

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

在 C++ 中 class 我有这样的东西(我尝试过不同的东西):

class X : public QObject  {
    Q_OBJECT

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

我在 qml 文件中试过,但它没有编译:

property var Qt.point points: []

我想将此数组从 QML(在 javascript 函数中)传递给 C++ 函数,如下所示:

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

如果我这样声明积分:

property var points: []

调用了C++函数,但是points为NULL。

property var Qt.point points: []

没有意义,语法是 属性 + 类型 + 名称。 Qt.point 不是类型,而是 returns 类型 point 对象的函数。而包含 JS 数组的 var 肯定不会在 C++ 中转换为 QObject *

我建议您将点数组实现为 C++ class,并使用访问函数来访问或操作 QML 中的数据。因此,您的 X class 中将有一个 QVector<QPoint>,以及获取大小、获取或设置点、追加、前置、插入等功能。因此,当您调用 save() 时,您可以直接访问 C++ 数据。那将是最有效的解决方案。