Close Keyboard on IOS

In the last week-end, while I was working on my side project, I discovered a problem: on iOS the keyboard stay opened after it’s opened due to a click in an input text. The obvious solution that I thought is:

Page {
      MouseArea {
                anchors.fill: parent
                enabled: Qt.inputMethod.visible
                onClicked: Qt.inputMethod.hide()
      }
      //.... input fields
}

Thus in this way, clicking/tapping on the screen, the keyboard should disappear. In my case it doesn’t work because the input fields are in a ScrollView (and if you move the MouseArea in the ScrollView the view doesn’t scroll).

My solution:

Page {
      MouseArea {
                anchors.fill: parent
                enabled: Qt.inputMethod.visible
                onClicked: Qt.inputMethod.hide()
                propagateComposedEvents: true
                z: 10
      }
      ScrollView {
        id: scrollView
        anchors.fill: parent
        anchors.bottomMargin: dp(60)
        clip: true
        contentWidth: -1

        ColumnLayout {
            id: colView
            //.... input fields
}

The tips&trick is set a Z value to the MouseArea and propagates the mouse event to the object in the bottom layers.

Leave a Comment