本页面

C++ 与 QML 代码之间的接口Qt Positioning

概述

Qt Positioning 使用两种方法简化 C++ 和 QML 代码之间的位置数据交换。

直接映射

Qt Positioning 允许您将以下值类型从 QML 传递到 C++,反之亦然:

基于 QVariant 的集成

本节提供有关如何集成QGeoAddressQGeoLocation 的信息。

地址 - QGeoAddress

Address.address 属性用于在 C++ 和 QML 代码之间提供接口。首先必须从 C++ 获取Address 对象的指针,然后使用property() 和setProperty() 函数获取和设置address 属性。

以下代码从 C++ 获取QGeoAddress 对象:

 QGeoAddress geoAddress = qmlObject->property("address").value<QGeoAddress>();

以下代码根据 C++ 中的QGeoAddress 对象设置 QML 对象的地址属性:

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.