我有一個QQuickItem
對應於一個MapPolyline
對象。多段線有一個名爲path
的屬性,在文檔中定義該屬性的類型爲list<coordinate>
。 coordinate
是一種在C++世界中映射到QGeoCoordinate
的類型。我想弄清楚如何從C++設置這個屬性的值。如何在C++中的QML對象上設置QJSValue類型的屬性?
如果我檢查QMetaObject
該項目並尋找它的path
屬性報告類型,它表示一個類型的QJSValue
。目前還不清楚我可以使用QObject::setProperty()
還是QQmlProperty::write()
來設置C++的這個值。我已經試過如下:
我試圖創建一個
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
。我還試圖餡點列表成
QVariantList
,這似乎是我可以在C++找到一個list<coordinate>
(QGeoCoordinate
能夠被放置在一個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); }
這也沒有工作;相同的結果。
這個過程似乎沒有很好的記錄。我需要使用什麼格式才能完成這項工作?
QVariantList方法應該工作。至少我認爲它的工作原理,如果你將它傳遞給QDeclarativePolylineMapItem :: setPath() –