How to detect if left mouse button is pressed in mouseArea in onPositionChanged?
Solved
QML and Qt Quick
-
Hi,
I have a mouseArea with hover enabled.
In the handler "onPositionChanged" I would like to detect if e.g. the left mouse button is pressed or not.
Somehow this is not working.
Could you please give me a hint?Thanks!
MWE:
import QtQuick 2.12 import QtQuick.Window 2.12 Window { width: 640 height: 480 visible: true title: qsTr("Hello World") Rectangle { width: 400; height: 400 anchors.centerIn: parent color: "green" MouseArea { anchors.fill: parent hoverEnabled: true onPositionChanged: { console.log(mouseX, mouseY) if(mouse.button == Qt.LeftButton) // Does not detect a pressed left mouse button! Why? console.log("Left pressed") else console.log("Left button not detected!") } } } }
-
Hi,
I have a mouseArea with hover enabled.
In the handler "onPositionChanged" I would like to detect if e.g. the left mouse button is pressed or not.
Somehow this is not working.
Could you please give me a hint?Thanks!
MWE:
import QtQuick 2.12 import QtQuick.Window 2.12 Window { width: 640 height: 480 visible: true title: qsTr("Hello World") Rectangle { width: 400; height: 400 anchors.centerIn: parent color: "green" MouseArea { anchors.fill: parent hoverEnabled: true onPositionChanged: { console.log(mouseX, mouseY) if(mouse.button == Qt.LeftButton) // Does not detect a pressed left mouse button! Why? console.log("Left pressed") else console.log("Left button not detected!") } } } }
@robro there are 2 properties you can check:
for any accepted Button to be pressed:
https://doc.qt.io/qt-5/qml-qtquick-mousearea.html#pressed-propTo know what exact button was pressed:
https://doc.qt.io/qt-5/qml-qtquick-mousearea.html#pressedButtons-prop