Qt Quick 文本输入处理和验证器

文本可视化类型

Qt Quick 提供了几种在屏幕上显示文本的类型。 类型将在屏幕上显示格式化文本, 类型将在屏幕上显示多行编辑,而 将在屏幕上显示单行可编辑字段。Text TextEdit TextInput

要进一步了解它们的具体功能和属性,请访问各自的文档。

验证输入文本

验证器类型强制执行TextInput 对象的类型和格式。

DoubleValidator

定义非整数的验证器

IntValidator

为整数值定义验证器

RegularExpressionValidator

提供字符串验证器

Column {
    spacing: 10

    Text {
        text: "Enter a value from 0 to 2000"
    }
    TextInput {
        focus: true
        validator: IntValidator { bottom:0; top: 2000}
    }
}

验证器类型绑定到TextInputvalidator 属性。

Column {
    spacing: 10

    Text {
        text: "Which basket?"
    }
    TextInput {
        focus: true
        validator: RegularExpressionValidator { regularExpression: /fruit basket/ }
    }
}

代码段中的正则表达式只允许输入文本为fruit basket

请注意,QML 解析的是 JavaScript 正则表达式,而 Qt Qml 的QRegularExpression 类的正则表达式基于 Perl 正则表达式。

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.