【问题标题】:QML Map visible regionQML Map 可见区域
【发布时间】:2016-02-12 14:45:23
【问题描述】:

在我的应用程序中,我使用QtLocation 来显示地图。由于只有 QML API 来渲染地图,这里是我的 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++ 代码中,我需要知道地图小部件中当前可见区域,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 获得可见矩形。

【问题讨论】:

    标签: c++ qt dictionary qml


    【解决方案1】:

    正确的语法是:

    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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-08
      • 2010-12-26
      • 1970-01-01
      • 1970-01-01
      • 2016-11-12
      相关资源
      最近更新 更多