【问题标题】:How to only allow the user type numbers and comma in the qml textField?如何只允许用户在 qml textField 中输入数字和逗号?
【发布时间】:2020-06-09 18:32:50
【问题描述】:

我想打印 textField 的值,但我希望用户只能输入数字、"," 和 /

TextField {
    id: textField1
    implicitWidth: 200
    implicitHeight: 30
}
Button{
    onClicked:{console,log(textField1.text)}
}

【问题讨论】:

    标签: qt qml


    【解决方案1】:

    为了回答您的问题,以下是可行的解决方案。

     TextField {
        id: textField1
        implicitWidth: 200
        implicitHeight: 30
        validator: RegExpValidator{regExp: /^[0-9,/]+$/}
    }
    
    Button{
        onClicked:{console.log(textField1.text)}
    }
    

    要防止用户输入内容,请在 Qt Found here 中使用文本验证器。它利用了正则表达式,所以你有更多的控制权。

    通过这样做,您可以完全阻止用户输入您不想要的字符。

    【讨论】:

      【解决方案2】:

      在安卓和ios设备上使用

      inputMethodHints : Qt.ImhFormattedNumbersOnly
      

      在 TextField 中仅在数字输入时打开键盘,即包括小数点和减号。 和其他 inputMethodHints [TextFiled QML][

      并在验证器中使用正则表达式来添加输入限制。

      示例: 验证器:RegExpValidator{regExp: /[0-9,/]*/}

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-09-22
        • 1970-01-01
        • 1970-01-01
        • 2021-11-18
        • 1970-01-01
        • 2022-12-04
        相关资源
        最近更新 更多