本页内容

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.