Qt は qml (QtLocation) を使用してマップを表示します

1. qtバージョンとQtLocationモジュールバージョンの確認

Qtのバージョンが低すぎるとQtLocationモジュールが存在しません 私のバージョンは以下の通りです
ここに画像の説明を挿入します
ビルドツールのバージョンは以下の通りです
ここに画像の説明を挿入します

2. qmlコードの記述

1. プロジェクトにモジュールを追加する

まずモジュールをプロジェクトに追加しますquickwidgets positioning location
ここに画像の説明を挿入します

2. リソースファイルを追加する

ここに画像の説明を挿入します

3. qml ファイルをリソース ファイルに追加します

ここに画像の説明を挿入します

4. qmlコードの書き方

import QtQuick
import QtLocation
import QtPositioning
import QtQuick.Controls
Rectangle {
    
    
    width: parent
    height: parent
    visible: true
    Control{
    
    
        id:labelcpp
        objectName: 'labelcpp'
        font.pointSize: 38
        property real latitudeSave: 22.64018
        property real longitudeSave: 113.92746
        //cpp调用这个函数
        function getText()
        {
    
    
            return  map.center + " zoom " + map.zoomLevel.toFixed(3)
                    + " min " + map.minimumZoomLevel + " max " + map.maximumZoomLevel
        }
        function setCoordinate(latitude,longitude)
        {
    
    
            latitudeSave = latitude
            longitudeSave = longitude
            map.center.latitude = latitude
            map.center.longitude = longitude
            map.update()
            console.log("latitude="+latitude+"   longitude="+longitude);
        }
    }


    Plugin {
    
    
        id: mapPlugin
        name: "osm"
//        PluginParameter { name: "osm.mapping.providersrepository.address"; value: "http://www.mywebsite.com/osm_repository" }
//        PluginParameter { name: "osm.mapping.highdpi_tiles"; value: true }
    }

    Map {
    
    
        id: map
        anchors.fill: parent
        plugin: mapPlugin
        center: QtPositioning.coordinate(22.64018, 113.92746) // Oslo
        zoomLevel: 14
        property geoCoordinate startCentroid

        PinchHandler {
    
    
            id: pinch
            target: null
            onActiveChanged: if (active) {
    
    
                map.startCentroid = map.toCoordinate(pinch.centroid.position, false)
            }
            onScaleChanged: (delta) => {
    
    
                map.zoomLevel += Math.log2(delta)
                map.alignCoordinateToPoint(map.startCentroid, pinch.centroid.position)
            }
            onRotationChanged: (delta) => {
    
    
                map.bearing -= delta
                map.alignCoordinateToPoint(map.startCentroid, pinch.centroid.position)
            }
            grabPermissions: PointerHandler.TakeOverForbidden
        }
        WheelHandler {
    
    
            id: wheel
            // workaround for QTBUG-87646 / QTBUG-112394 / QTBUG-112432:
            // Magic Mouse pretends to be a trackpad but doesn't work with PinchHandler
            // and we don't yet distinguish mice and trackpads on Wayland either
            acceptedDevices: Qt.platform.pluginName === "cocoa" || Qt.platform.pluginName === "wayland"
                             ? PointerDevice.Mouse | PointerDevice.TouchPad
                             : PointerDevice.Mouse
            rotationScale: 1/120
            property: "zoomLevel"
        }
        DragHandler {
    
    
            id: drag
            target: null
            onTranslationChanged: (delta) => map.pan(-delta.x, -delta.y)

        }
        Shortcut {
    
    
            enabled: map.zoomLevel < map.maximumZoomLevel
            sequence: StandardKey.ZoomIn
            onActivated: map.zoomLevel = Math.round(map.zoomLevel + 1)
        }
        Shortcut {
    
    
            enabled: map.zoomLevel > map.minimumZoomLevel
            sequence: StandardKey.ZoomOut
            onActivated: map.zoomLevel = Math.round(map.zoomLevel - 1)
        }
        Component.onCompleted: {
    
    
            map.addMapItem(circle)
        }
    }
    MapCircle {
    
    
        id: circle
        center: QtPositioning.coordinate(labelcpp.latitudeSave,labelcpp.longitudeSave)
        radius: 50
        border.width: 5

        //鼠标按住后可移动
        MouseArea {
    
    
            anchors.fill: parent
            drag.target: parent
        }
    }
}


ControlC++ とのデータ対話に使用されsetCoordinate、座標を送信して地図上の点を固定することにより、getText地図の中点と地図のズーム レベルを取得するために使用されます。

5. C++ と対話して座標点を送信します

void MainWindow::on_pushButton_clicked()
{
    
    
    QQuickItem *root = ui->quickWidget->rootObject();//拿到所有对象的列表
    auto labelqml = root->findChild<QObject*>("labelcpp");//名字要与main.qml中的 objectName: 'labelcpp' 相同
    QVariant ret;
    QMetaObject::invokeMethod(labelqml, "setCoordinate", Q_ARG(QVariant, 22.65599), Q_ARG(QVariant, 113.92576));
    qDebug() << ret.toString();
}

3.エフェクト表示

ここに画像の説明を挿入します

おすすめ

転載: blog.csdn.net/qq_15181569/article/details/132364456