Home > Back-end >  Qt Qml Reference Error mouse is not defined
Qt Qml Reference Error mouse is not defined

Time:12-16

I want to get mouse X and mouse Y on map. I use mouse.x but I get an error that "Reference Error mouse is not defined". In qt's signals I dont get error but in signals that I create I get error

Not error signal:

MouseArea {
    anchors.fill: parent
    acceptedButtons: Qt.LeftButton | Qt.RightButton
    onm ouseXChanged: {
        var coordinate = mapview.toCoordinate(Qt.point(mouse.x,mouse.y))
        store_coordinate.store(coordinate)
        mainwindow.mapClickSignal()
        if (mouse.button === Qt.RightButton)
        {
            mainwindow.rightClickedMenuFreeSpace(Qt.point(mouse.x,mouse.y),coordinate.latitude,coordinate.longitude)
        }
    }
    onPressed: {
        mouse.accepted = false
    }
}

Error signal:

Connections {
    target: mainwindow
    onSelectItem: {
        var coordinate = QtPositioning.coordinate(x, y)
        mapview.center =  coordinate
        mapview.update()
    }

    onRemoveAction: {
        var coordinate = mapview.toCoordinate(Qt.point(mouse.x,mouse.y))//error
        mainwindow.deleteAction(coordinate.latitude,coordinate.longitude)
    }
}

CodePudding user response:

Have a look at the signal documentation.

onMouseXChanged is a property change signal handler which does not have parameters. Therefore you need to use ids and access properties if available, e.g. mouseX/mouseY.

onPressed and onPositionChanged are normal signal handler which provide parameter. To access those, you should assign a function to the handler. Both arrow functions and anonymous functions work.

Depending on the target of your Connections object the same applies. You should have a look into the documentation of the target and look at the signals, figure out which parameters are provided.

import QtQuick

Window {
    id: root
    width: 400
    height: 300
    visible: true

    MouseArea {
        id: mouseArea
        anchors.fill: parent
        hoverEnabled: true
        acceptedButtons: Qt.LeftButton | Qt.RightButton

        onm ouseXChanged: {
            console.log("onMouseXChanged", Qt.point(mouseArea.mouseX, mouseArea.mouseY))
        }

        onPressed: function(mouse) {
            mouse.accepted = false
        }

        onPositionChanged: function(mouse) {
            console.log("onPositionChanged", Qt.point(mouse.x, mouse.y))
        }
    }


    Connections {
        target: mouseArea
        function onWheel(wheel) { console.log(wheel) }
    }
}

CodePudding user response:

The mouseEvent mouse is passed as a parameter to only some signals (e.g. onClicked, onPressed). However, the mouseEvent mouse parameter is not available in all signal handlers in MouseArea, so, I wouldn't expect it to be available in onMouseXChanged. Also, the mouse parameter/property is not defined outside of MouseArea.

Consider setting the id: mouseArea, i.e.

    MouseArea {
        id: mouseArea
    }

Then in your Connections you can make use of mouseArea.mouseX and mouseArea.mouseY.

  • Related