How to make QML TextField binding work under Android?

后端 未结 1 786
暗喜
暗喜 2021-01-22 19:59

I am currently trying to write a QML/C++ application for Android. Somewhere in my code, I have a class that looks like this:

class MyClass : public QObject
{
  Q         


        
相关标签:
1条回答
  • 2021-01-22 20:35

    As far as I understand the comments to my question, I have a few possibilities to solve this problem. The simplest one is to modify my QML item like this:

    ColumnLayout {
      TextField {
        id: myField
        placeholderText: qsTr("Enter text")
        Layout.fillWidth: true
        Binding {
          target: myClass
          property: "prop"
          value: myField.displayText
        }
    }
    

    If I didn't want to allow predictive text input, then I could just add the

    inputMethodHints: Qt.ImhNoPredictiveText
    

    on the TextField. In my case, I want predictive text input.

    Last, I could call QInputMethod::commit() when I click the button "Done" of my application, which will commit the displayed text on Android and then use the committed text in MyClass. In this case, I should not use QML's Binding feature. Instead, I should just commit the data to Android and then communicate my QML data to myClass.

    The simplest solution that best fits QML philosophy is probably to use displayText instead of text, which is the solution I am choosing.

    0 讨论(0)
提交回复
热议问题