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

查看:57
本文介绍了如何使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中键入文本我对此文本字段不满意",则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

在TextField上.就我而言,我想输入预想文本.

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

最后,当我单击应用程序的完成"按钮时,我可以调用QInputMethod :: commit(),它将在Android上提交显示的文本,然后在MyClass中使用提交的文本.在这种情况下,我不应该使用QML的Binding功能.相反,我应该将数据提交到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天全站免登陆