QValidatorclass provides validation of input text. 更多 …
继承者: QDoubleValidator , QIntValidator , QRegExpValidator , QRegularExpressionValidator
类本身是抽象的。2 个子类
QIntValidatorandQDoubleValidator,提供基本的数值范围校验,和QRegExpValidator使用自定义正则表达式提供一般校验。If the built-in validators aren’t sufficient, you can subclass
QValidator. The class has two virtual functions:validate()andfixup().
validate()must be implemented by every subclass. It returnsInvalid,中间体orAcceptabledepending on whether its argument is valid (for the subclass’s definition of valid).这 3 种状态需要一些解释。
Invalid字符串是 clearly 无效的。中间体is less obvious: the concept of validity is difficult to apply when the string is incomplete (still being edited).QValidator定义中间体作为字符串特性,作为最终结果这既不明显无效也不可接受。Acceptable意味着字符串可以作为最终结果被接受。有人可能会说,任何字符串都是合理中间状态,在输入期间Acceptable字符串是中间体.这里是一些范例:
对于接受 10 至 1000 (含 1000) 的整数的行编辑,42 和 123 是
Acceptable,空字符串和 5 是中间体, and “asdf” and 1114 isInvalid.对于接受 URL 的可编辑组合框,任何良好格式的 URL 是
Acceptable, “ http://example.com/ ,” is中间体(可能由于剪切和粘贴动作,意外在结尾加了个逗号),空字符串是中间体(the user might select and delete all of the text in preparation for entering a new URL) and “ http:///./ ” isInvalid.For a spin box that accepts lengths, “11cm” and “1in” are
Acceptable, “11” and the empty string are中间体, and “ http://example.com ” and “hour” areInvalid.
fixup()is provided for validators that can repair some user errors. The default implementation does nothing.QLineEdit,例如,会调用fixup()if the user presses Enter (or Return) and the content is not currently valid. This allows thefixup()function the opportunity of performing some magic to make anInvalidstringAcceptable.验证器拥有区域设置,设置采用
setLocale(). It is typically used to parse localized data. For example,QIntValidatorandQDoubleValidator使用它来剖析整数和双精度数的本地化表示。
QValidatoris typically used withQLineEdit,QSpinBoxandQComboBox.
PySide2.QtGui.QValidator.
State
¶
此枚举类型定义可以存在经过验证的字符串状态。
|
常量 |
描述 |
|---|---|
|
QValidator.Invalid |
字符串是 clearly 无效的。 |
|
QValidator.Intermediate |
字符串是合理的中间体值。 |
|
QValidator.Acceptable |
字符串是可接受的最终结果;即:它是有效的。 |
PySide2.QtGui.QValidator.
changed
(
)
¶
PySide2.QtGui.QValidator.
fixup
(
arg__1
)
¶
arg__1 – unicode
此函数试图改变
input
to be valid according to this validator’s rules. It need not result in a valid string: callers of this function must re-test afterwards; the default does nothing.
此函数的重实现可以改变
input
even if they do not produce a valid string. For example, an ISBN validator might want to delete every character except digits and “-“, even if the result is still not a valid ISBN; a surname validator might want to remove whitespace from the start and end of the string, even if the resulting string is not in the list of accepted surnames.
PySide2.QtGui.QValidator.
locale
(
)
¶
QLocale
返回用于验证器的区域设置。默认情况下,初始区域设置如同 QLocale()。
另请参阅
setLocale()
QLocale()
PySide2.QtGui.QValidator.
setLocale
(
locale
)
¶
locale
–
QLocale
设置
locale
that will be used for the validator. Unless has been called, the validator will use the default locale set with
setDefault()
. If a default locale has not been set, it is the operating system’s locale.
另请参阅
locale()
setDefault()
PySide2.QtGui.QValidator.
validate
(
arg__1
,
arg__2
)
¶
arg__1 – unicode
arg__2
–
int
PyObject
此虚函数返回
Invalid
if
input
is invalid according to this validator’s rules,
中间体
if it is likely that a little more editing will make the input acceptable (e.g. the user types “4” into a widget which accepts integers between 10 and 99), and
Acceptable
若输入有效。
函数可以改变
input
and
pos
(光标位置) 若要求的话。