Qt TextField is not invoking the keyboard when lau

2019-07-14 11:32发布

问题:

I have simple QML app, that shows only one TextField:

Rectangle {
    color: "#00000000"

    TextField {
        anchors.centerIn: parent
    }
}

I thought, that Android keyboard will be shown automatically, when I touch on TextField, but it's not happening. I tried to use focus, Qt.inputMethod.show(), forceActiveFocus() inside Component.onCompleted of TextField and many other methods, but keyboard not shown anyway. Tried to use app on other devices and Android 4.1-4.4 versions, but result always the same )))):

Is that a bug or incorrect TextField's setup?

回答1:

I`ve had similar problem, and it was in the

flags: Qt.FramelessWindowHint

in ApplicationWindow. Remove it for Android. It affects all text inputs in QML.

After removing you will get back proper android system event notifications (the Back key press for example) as a bonus.



回答2:

There is nothing wrong with your code. TextFiled {} item should launch the keyboard automatically when operated on android device. I created a simple "Qt Quick Controls Application" and added your code and it worked for me, but there are couple of points lacking, if you have not done it already:

1. You have not set the Width/Height properties of the item properly.
2. Set anchors to your Rectangle item.

I made slight modifications to your code as below:

Rectangle {
    color: "#00000000"
    anchors.fill: parent

    Row {
        anchors.centerIn: parent
        spacing: 10

        Text {
            id: textTitle
            text: "Text Field: "
            width: 100
            height: 100
            color: "black"
            font.pixelSize: 40
            horizontalAlignment: Text.AlignRight
            verticalAlignment: Text.AlignVCenter
        }

        TextField {
            id: textField

            width: 300
            height: 100
            text: ""
            font.pixelSize: 40
            placeholderText: qsTr("Enter Your Text")
            onEditingFinished: {
                console.log("Text entered: ", textField.text)
            }
        }
    }
}

I built the windows desktop version of this appliaction and verified that it works. Also, I have loaded this application on Nexus 6 Android emulator.

Very important point to note is the DPI (Physical Dots Per Inch) value of the android device on which you are going to load your application. Your Item's Width/Height and Text: Font Point size values should be set considering the DPI of the android device on which your application is going to get deployed. If not you may see your text appear very small on the real android device, and sometimes if not anchored properly it will hard to notice your text field on the application since you have just one textField item on the screen.

If not directly resolve your problem. Hope this helps you to debug and arrive at the solution.



标签: android qt qml qt5