c++ - QML map 可见区域

标签 c++ qt dictionary qml

在我的应用程序中,我正在使用QtLocation显示 map 。由于只有QML API可以呈现 map ,因此这是我的QML文件:

import QtQuick 2.0
import QtPositioning 5.5
import QtLocation 5.5

Item{
    anchors.fill: parent

    Plugin{
        id: osmplugin
        name: "osm"
    }

    Map {
        anchors.fill: parent
        id: map
        plugin: osmplugin;
        zoomLevel: (maximumZoomLevel - minimumZoomLevel)/2
        center {
            // The Qt Company in Oslo
            latitude: 59.9485
            longitude: 10.7686
        }
    }

    function bbox(){
        return map.visibleRegion;
    }
}

在C++代码中,我需要知道 map 小部件中当前可见的区域,QML Map具有属性visibleRegion http://doc.qt.io/qt-5/qml-qtlocation-map.html#visibleRegion-prop

但是我不知道如何从C++代码中获取它,因为QGeoShape是抽象的;

我尝试了这个:
    QQuickItem* map = mMap->rootObject();
    QGeoRectangle rect;
    bool ok = QMetaObject::invokeMethod( map, "bbox",  Qt::DirectConnection, Q_RETURN_ARG( QGeoRectangle, rect ) );
    if ( !ok )
        qDebug() << " Shit happens!";

    qDebug() << rect.isValid();

但这没有帮助。请告诉我如何从QML Map获得可见的矩形。

最佳答案

正确的语法是:

QQuickItem* map = mMap->rootObject();
QVariant ret;
bool ok = QMetaObject::invokeMethod( map, "bbox",  Qt::DirectConnection, Q_RETURN_ARG( QVariant, ret ) );
if ( !ok ){
    qWarning( "Fail to call qml method" );
}
QGeoRectangle rect = qvariant_cast<QGeoRectangle>( ret );
mNorth = rect.topLeft().latitude();
mSouth = rect.bottomLeft().latitude();
mWest  = rect.topLeft().longitude();
mEast  = rect.topRight().longitude();

关于c++ - QML map 可见区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33651748/

相关文章:

c++ - 来自线程的 Qt 信号导致错误

python - 使用 pandas python 中其他数据帧的值覆盖数据帧中的值

python - Python 字典键类型的权衡

C++ std::map 内存管理

java - 用于图像处理应用程序的 C++ 和 Java 通信

c++ - 顶点/索引缓冲区与 OpenGL 3.3 混淆

c++ - 为什么将子对象引用分配给父对象指针时无法访问子类

c++ - 用C++转置矩阵最快的方法是什么?

qt - 带有 id 的 Qml 中继器

QTableView - 限制所选项目的数量?