TextField 文本字段在 QML 上没有 return 值 | QT 5.12

TextField text field doesn't return value on QML | QT 5.12

我需要帮助。 所以这是我的代码:

TextField {
    id: passwordIDText
    placeholderText: "Insert text..."
    color: "Black"
    font.bold: true
    font.pixelSize: 21
    anchors.top: parent.top
    anchors.topMargin: parent.height / 10
    anchors.horizontalCenter: parent.horizontalCenter
    text: "aaa"
}

我使用 Android 8 (P20)、QT 12.4、Android NDK 20 和 Android SDK 28。 因此,当我在 android 屏幕上单击按钮时(我正在使用 20 进行测试),在调试中,为了获取 passwordIDText.text,该字段为空,为什么??但是如果我设置 text: "aaa" 硬编码,我会得到 passwordIDText.text = "aaa".

问题看TextField没有得到用户插入的值。我在屏幕上的 TextField 上插入了 123456,然后单击按钮:

MouseArea {
    anchors.fill: parent
    onClicked: {
        var test = passwordIDText.text;
        //passwordIDText.text is empty wtf !??

    }
}

有人遇到同样的问题吗?在较旧的 Android 版本中,TextField 工作正常。

我的配置:

我认为这可能是一个类似于 的问题,与预测文本在您需要访问时未提交有关。

尝试使用 passwordIDText.displayText 而不是 passwordIDText.text

或者,您可以关闭预测文本输入:

inputMethodHints: Qt.ImhNoPredictiveText

'ImhNoPredictiveText' 没有解决我在 Qt 5.14 中的问题。所以我用了

inputMethodHints: Qt.ImhSensitiveData

效果很好。

另一种方式,如果您不想禁用用户词典或单词预测,您应该在每次获取文本之前使用 deselect

我用过:

Button{
...
   onClicked:{
       focus = true
       console.log(textarea.text)
   }
...

}

TextArea{
    id: textarea
}

那么在 textarea.text

下确实可以访问文本

希望对您有所帮助!