qtqmlqtlocationmousearea

QtLocation QML: MouseArea stop Updating on hover


I have a qml map application and I added a marker as MapQuickItem. I have a lat lon display connected to map Mousearea, when I move the mouse on the map i read lat/lon in realtime, and all works well.

marker.qml is a mapquickItem with its Mousearea, when the mouse is "hover" the marker I write a string into the "labelLatLon" of the map. ALL Works well, but when I exited the mouse from the marker, the "labelLatLon" is no more been updated with the mouse coordinate, it stops updating lat lon. I move the mouse but no more lat/lon update... It seems the main MouseArea stop "hearing" mouse onhover.. This is the snipping code to test: the CROSS IMAGE is from resources.

    Rectangle {
        id: mainWindow
        
        visible: true
        
        Plugin {
            id: mapPlugin
            name: "osm"
            PluginParameter {
                name: "osm.mapping.providersrepository.disabled"
                value: "true"
            }
            PluginParameter {
                name: "osm.mapping.providersrepository.address"
                value: "http://maps-redirect.qt.io/osm/5.6/"
            }
        }       
        
        function addMarker(latitude,longitude) {
            
            var Component = Qt.createComponent("qrc:///qml/marker.qml")
            var item = Component.createObject(Item, {
                                                  coordinate: QtPositioning.coordinate(latitude, longitude)
                                              })
            map.addMapItem(item);         
        }
                
        function setLatLonBox(coordinate) {    
            labelLatLon.text= "Lat: %1; Lon:%2".arg(coordinate.latitude).arg(coordinate.longitude)  
        }
        
        Map {
            id: map
            
            gesture.enabled: true
            copyrightsVisible : true
            anchors.fill: parent
            plugin: mapPlugin
            center: QtPositioning.coordinate(44.0, 9.3) // La Spezia
            zoomLevel: 10
            Component.onCompleted:addMarker(44.0, 9.3)
            
            
            MouseArea  {
                id: mapMouseArea
                property int pressX : -1
                property int pressY : -1
                property int jitterThreshold : 10
                property int lastX: -1
                property int lastY: -1
                property var coordinate: map.toCoordinate(Qt.point(mouseX, mouseY))
                
                anchors.fill: parent
                acceptedButtons: Qt.LeftButton | Qt.RightButton
                hoverEnabled : true

                ColumnLayout {
                    id: layout
                    spacing: 5
                    z: 5  // ordine alto
                                        
                    Rectangle {
                        id: latLonArea
                        z: 5  // ordine alto
                        width: 320
                        height: 40
                        color: "grey"
                        opacity: 0.7
                        border.color: "black"
                        border.width: 1

                        Label {
                            id: labelLatLon
                            anchors.verticalCenter:  parent.verticalCenter
                            anchors.horizontalCenter:  parent.horizontalCenter
                            font.bold: true
                            color: "black"
                            text: "Lat: %1; Lon:%2".arg(mapMouseArea.coordinate.latitude).arg(mapMouseArea.coordinate.longitude)                            
                        }
                    }                    
                }
            }
        }
    }

and marker.qml

    MapQuickItem {
        id: marker
        z: 2  //ordine basso
        anchorPoint.x: marker.width / 2
        anchorPoint.y: marker.height /2
        
        property int idx
        
        sourceItem: Image{
            id: icon
            source: "../symbols/Red_Cross.png"
            sourceSize.width: 40
            sourceSize.height: 40
            opacity: markerMouseArea.pressed ? 0.6 : 1.0
        }
            
        MouseArea {
            id: markerMouseArea
            property int pressX : -1
            property int pressY : -1
            property int jitterThreshold : 10
            property int lastX: -1
            property int lastY: -1
            anchors.fill: parent
            acceptedButtons: Qt.LeftButton | Qt.RightButton
            hoverEnabled : true
            drag.target: marker
            
            onEntered: {
                var coordinate = map.toCoordinate(Qt.point(mouseX, mouseY));
                setLatLonBox(coordinate);
            }
        }
    }

Solution

  • Your problem is the label text property

    text: "Lat: %1; Lon:%2".arg(mapMouseArea.coordinate.latitude).arg(mapMouseArea.coordinate.longitude)
    

    Once you overwrite it with setLatLonBox, all the bindings on it are gone. You have to re-set it after you exit the MQI mouse area (or you re-enter map mouse area)