如何设置 QML MapPolyline 路径属性 [英] How to set QML MapPolyline Path Property

查看:36
本文介绍了如何设置 QML MapPolyline 路径属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 QtQuick 项目中有一个 MapPolyline 对象.我需要更改 C++ 文件中的路径属性,但我不知道该怎么做.

I have a MapPolyline object in my QtQuick Project. I need to change the path property from the C++ file, but I have no idea how to do that.

我的 qml - 文件:

MapPolyline {
    id: mapline
    objectName: "MapLine"
    line.width: 5
    line.color: "green"
    path:
    {[
        { latitude: 47.219791, longitude: 9.546032 },
        { latitude: 47.219657, longitude: 9.542508 },
        { latitude: 47.2194446, longitude: 9.5437876 }
    ]}
}

现在我想更改 C++ 文件中 path 属性的内容.

And now I want to change the content of the path property from the C++ file.

感谢您的帮助!

推荐答案

Qt Location 在 C++ 中工作正常.你不需要弄乱任何 QJSValue 的东西.在 QObject 中创建一个属性并返回一个 QVariantList.用 QGeoCoordinate(线中的点)填充 QVariantList.在 QML 中,将 MapPolyline 的路径设置为 QObject QVariantList 属性.QML 会自动将 QVariantList 转换为 Javascript 数组.QGeoCoordinates 是 qml 坐标类型,所以转换是透明的.每当您在 qml 中看到列表"类型时,如果您想填充 Javascript 对象(或创建 Q_GADGET),您始终可以从 c++ 或 QVariantMap 返回 QVariantList.

Qt Location works okay from c++. You don't need to mess with any QJSValue stuff. Create a property in your QObject and return a QVariantList. Fill the QVariantList with QGeoCoordinate (the points in your line). In QML set the MapPolyline's path to the QObject QVariantList property. QML will automatically convert the QVariantList into a Javascript Array. QGeoCoordinates are qml coordinate type so the conversion is transparent. Any time you see a "list" type in qml, you can always return QVariantList from c++ or QVariantMap if you want to populate a Javascript Object (or create a Q_GADGET).

让我知道的一件事是我从 C++ 返回的 QGeoCoordinates 无效.如果 QGeoCoordinates 无效,则 MapPolyLine 无法填充并会创建一条消息:不支持的路径类型".在将其添加到 QVariantList 之前,请确保 QGeoCoordinate isValid().

One thing that got me was that my QGeoCoordinates returned from c++ wasn't valid. If the QGeoCoordinates are invalid, the MapPolyLine fail to populate and will create a message: "Unsupported path type". Make sure QGeoCoordinate isValid() before adding it to the QVariantList.

这篇关于如何设置 QML MapPolyline 路径属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆