Filter mouse button press
-
Hello forum,
I need to do different operations based on different mouse press event - want to do rotation when the left mouse is pressed and do the zooming when the middle mouse button is pressed over the opengl scene that is rendered as an underlay.
Any hint?
-
Easy:
@
MouseArea {
anchors.fill: parent
acceptedButtons: Qt.LeftButton | Qt.MiddleButton
onClicked: {
switch(mouse.button) {
case Qt.LeftButton: console.log("rotate"); break
case Qt.MiddleButton: console.log("zoom"); break
default: console.log("default"); break
}
}
@ -
Should it not work onPositionChanged as well ? I am trying as follows, but there is no console output:
@
MouseArea
{
anchors.fill: root
//enable the mouse area
enabled: true
acceptedButtons: Qt.LeftButton | Qt.MiddleButton....................................... ....................................... onPositionChanged: { switch(mouse.button) { case Qt.LeftButton: currentMouseX = mouse.x currentMouseY = mouse.y ripple.rY = ripple.rY + (mouse.x - oldMouseX)/5.0 ripple.rX = ripple.rX + (mouse.y - oldMouseY)/5.0 oldMouseX = currentMouseX oldMouseY = currentMouseY console.log("Left button moved") break; case Qt.MiddleButton: break; default: break; } }
}
@Is there something missing up there ?
Thanks
-
Hi!
mouse.button holds the button that caused the event. So, positionChanged is not caused by any button, thus mouse.button is alway 0 in onPositionChanged.
You need to check mouse.buttons. Note the plural s. It is mouse.buttons. mouse.buttons holds a bitwise combination of all buttons that were pressed when the event occured.
@
onPositionChanged: {
console.log(mouse.buttons)
if (mouse.buttons & Qt.LeftButton)
console.log("rotate")
if (mouse.buttons & Qt.RightButton)
console.log("zoom")
}
@It's a bit tricky, but one gets used to it :-)