PySide6.QtGui.QIntValidator

class QIntValidator

QIntValidator 类提供了一个验证器,确保字符串包含指定范围内的有效整数。更多

PySide6.QtGui.QIntValidator 的继承图

概要

属性

  • bottomᅟ - 验证器的最低可接受值

  • topᅟ - 验证器的最高可接受值

方法

信号

注意

本文档可能包含从C++自动翻译到Python的代码片段。我们始终欢迎对代码片段翻译的贡献。如果您发现翻译问题,您也可以通过在我们的https:/bugreports.qt.io/projects/PYSIDE上创建工单来告知我们。

详细描述

警告

本节包含从C++自动翻译到Python的代码片段,可能包含错误。

使用示例:

validator = QIntValidator(100, 999, self)
edit = QLineEdit(self)
# the edit lineedit will only accept integers between 100 and 999
edit.setValidator(validator)

下面我们展示一些验证器的示例。在实际应用中,它们通常会像上面的示例那样与一个小部件相关联。

str = QString()
pos = 0
v = QIntValidator(100, 900, self)
str = "1"
v.validate(str, pos) # returns Intermediate
str = "012"
v.validate(str, pos) # returns Intermediate
str = "123"
v.validate(str, pos) # returns Acceptable
str = "678"
v.validate(str, pos) # returns Acceptable
str = "999"
v.validate(str, pos) # returns Intermediate
str = "1234"
v.validate(str, pos) # returns Invalid
str = "-123"
v.validate(str, pos) # returns Invalid
str = "abc"
v.validate(str, pos) # returns Invalid
str = "12cm"
v.validate(str, pos) # returns Invalid

请注意,值 999 返回 Intermediate。由等于或小于最大值的数字组成的值被视为中间值。这是有意为之的,因为阻止数字在范围内的数字不一定是最后输入的数字。这也意味着中间数字可以有前导零。

最小值和最大值可以通过一次调用setRange()来设置,或者分别使用setBottom()setTop()来设置。

QIntValidator 使用其 locale() 来解释数字。例如,在阿拉伯语环境中,QIntValidator 将接受阿拉伯数字。

注意

设置在locale()上的QLocale::NumberOptions也会影响数字的解析方式。例如,由于默认情况下未设置QLocale::RejectGroupSeparator,验证器将接受分组分隔符。因此,建议使用QLocale::toInt()来获取数值。

另请参阅

QDoubleValidator QRegularExpressionValidator toInt()Line Edits Example

注意

当使用from __feature__ import true_property时,属性可以直接使用,否则通过访问器函数使用。

property bottomᅟ: int

此属性保存验证器的最低可接受值。

默认情况下,此属性的值源自可用的最小有符号整数(-2147483648)。

另请参阅

setRange()

Access functions:
property topᅟ: int

此属性保存验证器的最高可接受值。

默认情况下,此属性的值源自可用的最大有符号整数(2147483647)。

另请参阅

setRange()

Access functions:
__init__([parent=None])
Parameters:

父对象QObject

构造一个带有parent对象的验证器,该对象接受所有整数。

__init__(bottom, top[, parent=None])
Parameters:
  • bottom – int

  • top – int

  • parentQObject

构建一个带有parent的验证器,该验证器接受从minimummaximum(包括这两个值)的整数。

bottom()
Return type:

整数

另请参阅

setBottom()

属性 bottomᅟ 的获取器。

bottomChanged(bottom)
Parameters:

底部 – int

属性 bottomᅟ 的通知信号。

setBottom(arg__1)
Parameters:

arg__1 – 整数

另请参阅

bottom()

属性 bottomᅟ 的设置器。

setRange(bottom, top)
Parameters:
  • bottom – int

  • top – int

设置验证器的范围,仅接受介于bottomtop之间的整数(包括这两个值)。

setTop(arg__1)
Parameters:

arg__1 – 整数

另请参阅

top()

属性 topᅟ 的设置器。

top()
Return type:

整数

另请参阅

setTop()

属性 topᅟ 的获取器。

topChanged(top)
Parameters:

top – int

属性 topᅟ 的通知信号。