内容表

上一话题

QTransform

下一话题

QVector2D

QValidator

QValidator class provides validation of input text. 更多

Inheritance diagram of PySide2.QtGui.QValidator

继承者: QDoubleValidator , QIntValidator , QRegExpValidator , QRegularExpressionValidator

概要

函数

虚函数

信号

详细描述

类本身是抽象的。2 个子类 QIntValidator and QDoubleValidator ,提供基本的数值范围校验,和 QRegExpValidator 使用自定义正则表达式提供一般校验。

If the built-in validators aren’t sufficient, you can subclass QValidator . The class has two virtual functions: validate() and fixup() .

validate() must be implemented by every subclass. It returns Invalid , 中间体 or Acceptable depending 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 is Invalid .

  • 对于接受 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:///./ ” is Invalid .

  • For a spin box that accepts lengths, “11cm” and “1in” are Acceptable , “11” and the empty string are 中间体 , and “ http://example.com ” and “hour” are Invalid .

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 the fixup() function the opportunity of performing some magic to make an Invalid string Acceptable .

验证器拥有区域设置,设置采用 setLocale() . It is typically used to parse localized data. For example, QIntValidator and QDoubleValidator 使用它来剖析整数和双精度数的本地化表示。

QValidator is typically used with QLineEdit , QSpinBox and QComboBox .

class QValidator ( [ parent=None ] )
param parent

QObject

设置验证器。 parent 参数被传递给 QObject 构造函数。

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 (光标位置) 若要求的话。