继承者: QRegExpValidator , QDoubleValidator , QIntValidator
PySide.QtGui.QValidator class provides validation of input text.
类本身是抽象的。2 个子类 PySide.QtGui.QIntValidator and PySide.QtGui.QDoubleValidator ,提供基本的数值范围校验,和 PySide.QtGui.QRegExpValidator 使用自定义正则表达式提供一般校验。
If the built-in validators aren't sufficient, you can subclass PySide.QtGui.QValidator . The class has two virtual functions: PySide.QtGui.QValidator.validate() and PySide.QtGui.QValidator.fixup() .
PySide.QtGui.QValidator.validate() must be implemented by every subclass. It returns Invalid , 中间体 or Acceptable 取决于其自变量是否有效 (对于有效的子类定义而言)。
这 3 种状态需要一些解释。 Invalid 字符串是 clearly 无效的。 中间体 is less obvious: the concept of validity is difficult to apply when the string is incomplete (still being edited). PySide.QtGui.QValidator 定义 中间体 作为字符串特性,作为最终结果这既不明显无效也不可接受。 Acceptable 意味着字符串可以作为最终结果被接受。有人可能会说,任何字符串都是合理中间状态,在输入期间 Acceptable 字符串是 中间体 .
这里是一些范例:
PySide.QtGui.QValidator.fixup() is provided for validators that can repair some user errors. The default implementation does nothing. PySide.QtGui.QLineEdit ,例如,会调用 PySide.QtGui.QValidator.fixup() if the user presses Enter (or Return) and the content is not currently valid. This allows the PySide.QtGui.QValidator.fixup() function the opportunity of performing some magic to make an Invalid string Acceptable .
验证器拥有区域设置,设置采用 PySide.QtGui.QValidator.setLocale() . It is typically used to parse localized data. For example, PySide.QtGui.QIntValidator and PySide.QtGui.QDoubleValidator 使用它来剖析整数和双精度数的本地化表示。
PySide.QtGui.QValidator is typically used with PySide.QtGui.QLineEdit , PySide.QtGui.QSpinBox and PySide.QtGui.QComboBox .
| 参数: | parent – PySide.QtCore.QObject |
|---|
设置验证器。 parent 参数被传递给 PySide.QtCore.QObject 构造函数。
此枚举类型定义可以存在经过验证的字符串状态。
| 常量 | 描述 |
|---|---|
| QValidator.Invalid | 字符串是 clearly 无效的。 |
| QValidator.Intermediate | 字符串是合理的中间体值。 |
| QValidator.Acceptable | 字符串是可接受的最终结果;即:它是有效的。 |
| 参数: | arg__1 – unicode |
|---|
此函数试图改变 input 为有效根据此验证器的规则。它不需要产生有效字符串:此函数的调用者之后必须重新测试;默认什么都不做。
此函数的重实现可以改变 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.
| 返回类型: | PySide.QtCore.QLocale |
|---|
返回用于验证器的区域设置。默认情况下,初始区域设置如同 QLocale()。
另请参阅
PySide.QtGui.QValidator.setLocale() QLocale.QLocale()
| 参数: | locale – PySide.QtCore.QLocale |
|---|
设置 locale 将用于验证器。除非已调用 setLocale,否则验证器将使用默认区域设置,设置采用 QLocale.setDefault() . If a default locale has not been set, it is the operating system's locale.
另请参阅
PySide.QtGui.QValidator.locale() QLocale.setDefault()
| 参数: |
|
|---|---|
| 返回类型: |
PyObject |
此虚函数返回 Invalid if input 是无效的根据此验证器规则, 中间体 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 (光标位置) 若要求的话。