如何使 QML TextField 绑定在 Android 下工作? [英] How to make QML TextField binding work under Android?

查看:31
本文介绍了如何使 QML TextField 绑定在 Android 下工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试为 Android 编写 QML/C++ 应用程序.在我的代码中,我有一个如下所示的类:

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_OBJECT

  Q_PROPERTY(QString prop READ prop WRITE setProp NOTIFY propChanged)

public:
  explicit MyClass(QObject* a_Parent = 0);

  QString prop() const
  {
    return m_Prop;
  }

  void setProp(const QString& a_Value)
  {
    m_Prop = a_Value;
    emit propChanged();
  }

signals:
  void propChanged();

private:
  QString m_Prop;
};

在我的 QML 内容中,我有一个按以下方式定义的项目:

Somwhere in my QML stuff, I have an Item defined in the following way:

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

当然,我做了一切必要的事情来使 MyClass 类型的对象 myClass 在 C++ 和 QML 之间共享(作为上下文属性).

Of course, I did everything necessary to make the object myClass of type MyClass shared between C++ and QML (as a context property).

现在,问题如下:无论我在上面的文本字段中输入什么,在 Android 下,最后一个词永远不会从 myField.text 传递到 MyClass.m_Prop.例如,如果我在 TextField 中键入文本我对此 TextField 不满意",则 myClass.m_Prop 将包含我对此不满意".但是,如果我键入我对此 TextField 不满意"(注意空格),然后删除最后一个空格,则所需的字符串将存储在 m_Prop 变量中.

Now, the problem is the following: whatever I'm typing in the above textfield, under Android, the last word is never communicated from myField.text to MyClass.m_Prop. For example, if I type the text "I am not happy with this TextField" in the TextField, then myClass.m_Prop will contain "I am not happy with this". However, if I type "I am not happy with this TextField " (notice the space) and then remove the last space, then the desired string is stored in the m_Prop variable.

奇怪的是,这在 Linux 下运行得非常好.有人能告诉我我在这里想念什么吗?我正在使用 Qt 5.7 和 Android 6.0.

Strange is, that this works perfectly fine under Linux. Can someone tell me what I am missing here? I am working with Qt 5.7 and Android 6.0.

感谢您的帮助...

推荐答案

据我了解对我的问题的评论,我有几种可能解决这个问题.最简单的一种是像这样修改我的 QML 项:

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.

最后,当我单击应用程序的完成"按钮时,我可以调用 QInputMethod::commit(),这将在 Android 上提交显示的文本,然后在 MyClass 中使用提交的文本.在这种情况下,我不应该使用 QML 的绑定功能.相反,我应该将数据提交给 Android,然后将我的 QML 数据传递给 myClass.

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.

最适合 QML 哲学的最简单的解决方案可能是使用 displayText 而不是 text,这是我选择的解决方案.

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

这篇关于如何使 QML TextField 绑定在 Android 下工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆