onPressAndHold not working with Qt.TapAndHoldGesture
-
Hello, I'm having this issue on an android/ios, on Qt 5.14.2
I'm trying to capture pressAndHold action using MouseArea object, but skip capturing click action.Using code provided bellow:
Window { width: 640 height: 480 visible: true title: qsTr("Hello World") MouseArea { anchors.fill: parent acceptedButtons: Qt.TapAndHoldGesture onClicked: console.log("onClicked") onPressed: console.log("onPressed") onPressAndHold: console.log("onPressAndHold") } }
when I try to click, press, or press and hold, It doesn't seem to register any of these 3 actions.
If I change acceptedButtons to Qt.TapGesture, it then captures all 3 actions successfully.Is there any way to capture only onPressAndHold action, but to skip capturing onClicked action, so it could be used by other objects?
-
@Augustas said in onPressAndHold not working with Qt.TapAndHoldGesture:
Hello, I'm having this issue on an android/ios, on Qt 5.14.2
I'm trying to capture pressAndHold action using MouseArea object, but skip capturing click action.Using code provided bellow:
acceptedButtons: Qt.TapAndHoldGesture
The problem with this code is that acceptedButtons is a flag that indicates which mouse buttons are accepted, not which signals will be emitted.
Qt.TapAndHoldGesture = 2, which is the same as Qt.RightButton.
Qt.TapGesture = 1, which is also the value of Qt.LeftButton.
As a result, the code above is instructing the MouseArea to only accept right mouse buttons.To ignore the clicked signal, set the accepted property of the event to false. Clicked is a composed event that relies on the pressed event. For the parent to see a click, either the child must refuse that event as well (disabling pressAndHold in the process), or propagateComposedEvents must be set to true.
MouseArea { MouseArea { propagateComposedEvents: true onPressed: console.log("child press") onClicked: (mouse)=> { mouse.accepted = false; } onPressAndHold: console.log("child press and hold") } onClicked: console.log("parent clicked") }
-
@jeremy_k the code you have provided works great with 2 MouseArea objects.
However I'm trying to use TextField as a parent object and MouseArea as a child object. This is where I'm encountering issues.Regarding acceptedButtons property, I'd assume that the buttons that are accepted, emit the appropriate signals when clicked. That's how it works when using Qt.LeftButton/Qt.RightButton values. Here's a minimal example:
/Both objects occupy the same space TextEdit { id: textEdit anchors.fill: parent selectByMouse: true readOnly: true text: "Some text here" //Left mouse click funcionality onCursorPositionChanged: console.log("Cursor pos: " + cursorPosition) MouseArea { anchors.fill: parent acceptedButtons: Qt.RightButton //Right mouse click funcionality onClicked: console.log("tap and hold clicked") } }
However, I'm trying to implemenet the same funcionality on a mobile device, and since it doesn't have left/right mouse buttons, I'm trying to use Clicked/OnPressAndHold events, which as I've found out, have different funcionality. Here's a better example of my desired case, than the first one I've provided:
//Both objects occupy the same space TextEdit { id: textEdit anchors.fill: parent selectByMouse: true readOnly: true text: "Some text here" //Left mouse click funcionality onCursorPositionChanged: console.log("Cursor pos: " + cursorPosition) MouseArea { anchors.fill: parent acceptedButtons: Qt.TapAndHoldGesture //Right mouse click funcionality onPressAndHold: console.log("tap and hold clicked") } }
In this example, the PressAndHold event is never registered (because acceptedButtons is set to Qt.TapAndHoldGesture, which is.. interesting). If I remove the acceptedButtons line, PressAndHold is registered successfully, but TextEdit objects onCursorPositionChanged no longer gets registered.
I've tried testing around with the code you've provided, however if I set either MouseArea object onClicked event to (mouse)=> { mouse.accepted = false; }, PressAndHold event doesn't work anymore.As far as I've tested, there's no way to have my desired funcionality (catch a tap on a TextEdit object and any other gesture in the same space, as I was trying to do with a MouseArea using PressAndHold) , so I would be open to any suggestions.
-
@Augustas said in onPressAndHold not working with Qt.TapAndHoldGesture:
In this example, the PressAndHold event is never registered (because acceptedButtons is set to Qt.TapAndHoldGesture, which is.. interesting).
Why is that interesting? You already know that acceptedButtons treats TapAndHoldGesture as RightButton.
As far as I've tested, there's no way to have my desired funcionality (catch a tap on a TextEdit object and any other gesture in the same space, as I was trying to do with a MouseArea using PressAndHold) , so I would be open to any suggestions.
TextArea takes focus when it receives a press, rather than a click. PressAndHold requires that the MouseArea accept the press, meaning that it can not be ignored and allowed to automatically propagate to the parent item. One solution is to use the onClicked handler to transfer focus.
TextArea { MouseArea { onClicked: parent.focus = true onPressAndHold: print("PressAndHold is accepted") } }
-
@jeremy_k said in onPressAndHold not working with Qt.TapAndHoldGesture:
TextArea takes focus when it receives a press, rather than a click.
That's a good idea. I've tried playing around with focus, however it seems no matter how I set it, that doesn't change objects funcionality.