Qt Quick 文本输入处理与验证器
文本可视化类型
Qt Quick 提供了多种类型用于在屏幕上显示文本。Text 类型会在屏幕上显示格式化后的文本,TextEdit 类型会在屏幕上放置一个多行文本编辑框,而TextInput 则会在屏幕上放置一个单行可编辑文本框。
如需了解这些控件的具体功能和属性,请查阅相应的文档。
验证输入文本
验证器类型用于强制执行TextInput 对象的类型和格式。
定义了一个用于非整数值的验证器 | |
定义用于整数值的验证器 | |
提供一个字符串验证器 |
Column {
spacing: 10
Text {
text: "Enter a value from 0 to 2000"
}
TextInput {
focus: true
validator: IntValidator { bottom:0; top: 2000}
}
}验证器类型绑定到TextInput 的validator 属性。
Column {
spacing: 10
Text {
text: "Which basket?"
}
TextInput {
focus: true
validator: RegularExpressionValidator { regularExpression: /fruit basket/ }
}
}代码片段中的正则表达式仅允许输入的文本为fruit basket 。
请注意,QML解析的是JavaScript正则表达式,而Qt的QRegularExpression 类的正则表达式基于Perl正则表达式。
© 2026 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.