Search code examples
qtqmlclicktextfieldqt6

How to make TestField behave the same as MouseArea on click events


I'm on QT 6.6.1 under Windows 64 bits, I have found this:

MouseArea {
    onClicked: { console.log("clicked") }
    onPressAndHold: { console.log("pressAndHold") }
    onReleased: { console.log("released") }
}

when holding, it only shows "pressAndHold" (it skips the initial click), and on release, it shows "released", and on single click, it shows "clicked" and "released". This is correct behaviour if you ask me.

TextField {
    onPressed: { console.log("pressed") }
    onPressAndHold: { console.log("pressAndHold") }
    onReleased: { console.log("released") }
}

But when using a `TextField, when holding it first, it shows "press", and after a delay, it shows "pressAndHold", and on release, it does not show anything (it should show "released"), and on single click, it shows "pressed" and "released".

In best case scenario, I would like the TextField to behave the same way as MouseArea when clicking (so skip initial click if "pressAndHold", and "release" when releasing the mouse button).

Was this difference meant by design?

Is there a way to set the TextField mouse clicks events the same way as MouseArea?

This behaviour is in 6.6.1, win64 build, tested on Android emulator as well as MinGW.


Solution

  • This is indeed a bit surprising. You can get a 'onReleased' event after a hold event if you do not accept the pressAndHold:

    TextField {
        onPressed: { console.log("pressed") }
        onPressAndHold: event => { 
             // do not accept this event
             event.accepted = false; 
             console.log("pressAndHold")
        }
        onReleased: { console.log("released") }
    }