Qt定位中C++与QML代码的接口

Interfaces between C++ and QML Code in Qt Positioning

Qt定位中C++与QML代码的接口

Overview

概述

Qt Positioning utilizes two methods to simplify exchange of position data between C++ and QML code.

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

Direct C++ Value Integration in QtPositioning

Qt定位中的C++直接值集成

Starting with Qt 5.5, it has become much easier to integrate non-QObject based data types into QML. This is achieved by adding Q_GADGET support to QtQml. The macro converts classes into a light-weight version of a QObject without the required QObject inheritance. At the same time, it retains the reflection capabilities of QMetaObject. As a result, they can be directly exposed to QML.

​从Qt 5.5开始,将非基于QObject的数据类型集成到QML中变得更加容易。这是通过在QtQml中添加Q_GADGET支持来实现的。宏将类转换为QObject的轻量级版本,而不需要QObject继承。同时,它保留了QMetaObject的反射功能。因此,他们可以直接暴露给QML。

A significant number of Position related data types were converted to Q_GADGETs. They retain their API and value type character but have become introspectable via QMetaObject.

​大量与位置相关的数据类型被转换为Q_GADGETs。它们保留了API和值类型字符,但通过QMetaObject变得可内省。

The QML_ANONYMOUS macro is used to expose these types to the QML environment. See the QQmlEngine documentation for more details and the full list of available macros.

​QML_ANONYMOUS宏用于将这些类型暴露给QML环境。有关更多详细信息和可用宏的完整列表,请参阅QQmlEngine文档。

The classes, however, are not directly extended with this macro, because we do not want Qt Positioning to depend on QtQml. So a helper class is created for each of them, and the QML_FOREIGN macro is used:

​然而,这些类并没有直接用这个宏扩展,因为我们不希望Qt Positioning依赖于QtQml。因此,为每个对象创建了一个辅助类,并使用QML_FOREIGN宏:

struct QGeoCoordinateForeign
{
    Q_GADGET
    QML_FOREIGN(QGeoCoordinate)
    QML_ANONYMOUS
    QML_ADDED_IN_VERSION(5, 0)
};

The above registration of Positioning types is automatically done once by the QtPositioning QML plugin.

​上述定位类型的注册由QtPosition QML插件自动完成一次。

QVariant Based integration

基于QVariant的集成

This section provides information on how to integrate QGeoAddress and QGeoLocation.

​本节提供有关如何集成QGeoAddress和QGeoLocation的信息。

Address - QGeoAddress

The Address.address property is used to provide an interface between C++ and QML code. First a pointer to an Address object must be obtained from C++, then the property() and setProperty() functions must be used to get and set the address property.

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

The following piece of code gets the QGeoAddress object from C++:

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

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

The following piece of code sets the address property of the QML object based on a QGeoAddress object from C++:

​以下代码基于C++中的QGeoAddress对象设置QML对象的地址属性:

​qmlObject->setProperty("address", QVariant::fromValue(geoAddress));

Location - QGeoLocation

The Location.location property is used to provide an interface between C++ and QML code. First a pointer to a Location object must be obtained from C++, then the property() and setProperty() functions must be used to get and set the location property.

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

The following piece of code gets the QGeoLocation object from C++:

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

QGeoLocation geoLocation = qmlObject->property("location").value<QGeoLocation>();

The following piece of code sets the location property of the QML object based on a QGeoLocation object from C++:

​以下代码基于C++中的QGeoLocation对象设置QML对象的位置属性:

qmlObject->setProperty("location", QVariant::fromValue(geoLocation));

Positioning (C++)Android support

© 2024 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值