C++ 与 QML 代码之间的接口Qt Positioning
概述
Qt Positioning 通过两种方法简化了 C++ 与 QML 代码之间的位置数据交换。
直接映射
Qt Positioning 允许您在 QML 和 C++ 之间双向传递以下值类型:
| C++ 类 | QML 值类型 |
|---|---|
| QGeoCircle | geoCircle |
| QGeoCoordinate | geoCoordinate |
| QGeoPath | geoPath |
| QGeoPolygon | geoPolygon |
| QGeoRectangle | geoRectangle |
| QGeoSatelliteInfo | geoSatelliteInfo |
| QGeoShape | geoShape |
基于 QVariant 的集成
本节介绍了如何集成QGeoAddress 和QGeoLocation 。
地址 - QGeoAddress
Address.address 属性用于在C++和QML代码之间提供接口。首先必须从C++中获取Address 对象的指针,然后必须使用property()和setProperty()函数来获取和设置address 属性。
以下代码片段从 C++ 中获取QGeoAddress 对象:
QGeoAddress geoAddress = qmlObject->property("address").value<QGeoAddress>();以下代码片段基于来自 C++ 的QGeoAddress 对象,设置 QML 对象的 address 属性:
qmlObject->setProperty("address", QVariant::fromValue(geoAddress));位置 - QGeoLocation
Location.location 属性用于在C++和QML代码之间提供接口。首先必须从C++中获取Location 对象的指针,然后使用property()和setProperty()函数来获取和设置location 属性。
以下代码片段从 C++ 中获取QGeoLocation 对象:
QGeoLocation geoLocation = qmlObject->property("location").value<QGeoLocation>();以下代码片段根据来自 C++ 的QGeoLocation 对象设置 QML 对象的 location 属性:
qmlObject->setProperty("location", QVariant::fromValue(geoLocation));© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.