IntValidator QML Type
定义整数值的验证器。更多
| Import Statement: | import QtQuick |
| In C++: | QIntValidator |
属性
详细说明
IntValidator 类型为整数值提供了一个验证器。
如果未设置locale ,IntValidator 将使用default locale 来解释数字,并接受特定于本地的数字、组分隔符以及正负号。此外,IntValidator 始终保证接受按照 "C "区域格式化的数字。
下面的示例显示了一个带有 IntValidator 的TextInput 对象,用于检查用户是否输入了指定范围内的整数,并更新文本颜色以突出显示无效输入:
import QtQuick TextInput { focus: true validator: IntValidator { bottom: 0 top: 100 } color: acceptableInput ? "black" : "red"; }
验证器将防止提交不可能有效的文本。不过,在编辑时,只有容易识别的无效输入才会被阻止,例如符号冲突或过多的非零数字。这有时会令人惊讶。例如,上例中的验证器允许输入 "999",因为由等于或小于最大值的数字组成的值被视为 "中间值"--这意味着它们处于无效状态,但可以调整为有效。这样做的目的是,导致数字不在范围内的数字不一定是最后输入的数字。这也意味着中间数字可以有前导零。
基于TextInput 的acceptableInput 属性添加一个可视化指示器,可以让用户清楚地知道他们输入的数字是否会被接受。
另请参阅 DoubleValidator,RegularExpressionValidator, 以及验证输入文本。
属性文档
bottom : int
该属性保存验证器的最低可接受值。默认情况下,该属性的值取自可用的最低带符号整数(通常为 -2147483647)。
locale : string
该属性包含用于解释数字的语言名称。
另请参阅 Qt.locale()。
top : int
此属性保存验证器的最高可接受值。默认情况下,该属性的值来自可用的最高带符号整数(通常为 2147483647)。
© 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.