我有一个QQuickItem对应于一个MapPolyline对象的。折线有一个名为 的属性path,在文档中定义为 type list<coordinate>。coordinate是一种映射到QGeoCoordinateC++ 世界的类型。我试图弄清楚如何从 C++ 设置这个属性的值。
如果我检查该QMetaObject项目并查找它为该path属性报告的类型,它表示类型为QJSValue. 我不清楚如何从 C++ 中使用QObject::setProperty()或设置这个值QQmlProperty::write()。我尝试了以下方法:
我尝试创建一个
QJSValue数组类型,每个元素都保存我想要的坐标值,如下所示:void set_property_points(QQuickItem *item, const QVector<QGeoCoordinate> &pointList) { // Get the QML engine for the item. auto engine = qmlEngine(item); // Create an array to hold the items. auto arr = engine->newArray(pointList.size()); // Fill in the array. for (int i = 0; i < pointList.size(); ++i) arr.setProperty(i, engine->toScriptValue(pointList[i])); // Apply the property change. item->setProperty("path", arr.toVariant()); }这没有用;调用
setProperty()返回false。我还尝试将点列表填充到 a
QVariantList中,这似乎是我在 C++ 中为 a 找到的最佳匹配list<coordinate>(QGeoCoordinate能够放置在 a 中QVariant):/// Apply a list of `QGeoCoordinate` points to the specified `QQuickItem`'s property. void set_property_points(QQuickItem *item, const QVector<QGeoCoordinate> &pointList) { QVariantList list; for (const auto &p : pointList) list.append(QVariant::fromValue(p)); item->setProperty("path", list); }这也不起作用;相同的结果。
这个过程似乎没有很好的记录。我需要将数据放入什么格式才能完成这项工作?