2017-06-12 237 views
0

我有一個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); 
    } 
    

    這也沒有工作;相同的結果。

這個過程似乎沒有很好的記錄。我需要使用什麼格式才能完成這項工作?

+0

QVariantList方法應該工作。至少我認爲它的工作原理,如果你將它傳遞給QDeclarativePolylineMapItem :: setPath() –

回答

1

原來,未在文件中提到的第三個方法實際上似乎工作。我需要設置這樣的屬性:

QJSValue arr; // see above for how to initialize `arr` 
item->setProperty("path", QVariant::fromValue(arr)); 
相關問題