Some of the providing QtPositioning QML types providing interfaces to access and modify properties in C++.
Depending on the type of C++ class QtPositioning utilizes two methods to simplify exchange of position data between C++ and QML code.
Starting with Qt 5.5, it has become much easier to integrate non-
QObjectbased data types into QML. This is achieved by addingQ_GADGETsupport to QtQml . The macro converts classes into a light-weight version of aQObjectwithout the requiredQObjectinheritance. At the same time it retains the reflection capabilities ofQMetaObject. As a result they can be directly exposed to QML and do not require any further wrapper classes.A significant number of Position and Location related data types were converted to Q_GADGETs. They retain their API and value type character but have become introspectable via
QMetaObject. This conversion was done to the following classes:使用
QGeoCoordinateas an example, the C++ types are directly exposed to the QML environment via its meta type:qRegisterMetaType<QGeoCoordinate>(); QMetaType::registerEqualsComparator<QGeoCoordinate>();The above registration of
QGeoCoordinateis automatically done once by the QtPositioning QML plugin. The Plane Spotter example demonstrates this feature.
This section provides information on how to integrate
QGeoAddressandQGeoLocation.
Address.address property is used to provide an interface between C++ and QML code. First a pointer to a Address object must be obtained from C++, then use the
property()andsetProperty()functions to get and set theaddressproperty. The following gets theQGeoAddressrepresenting this object from C++:QGeoAddress geoAddress = qmlObject->property("address").value<QGeoAddress>();The following sets the properties of this object based on a
QGeoAddressobject from C++:qmlObject->setProperty("address", QVariant::fromValue(geoAddress));
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 use the
property()andsetProperty()functions to get and set thelocationproperty. The following gets theQGeoLocationrepresenting this object from C++:QGeoLocation geoLocation = qmlObject->property("location").value<QGeoLocation>();The following sets the properties of this object based on a
QGeoLocationobject from C++:qmlObject->setProperty("location", QVariant::fromValue(geoLocation));