• PySide 模块
  • PySide.QtGui
  • 内容表

    上一话题

    QPanGesture

    下一话题

    QRegExpValidator

    QValidator

    继承者: 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 字符串是 中间体 .

    这里是一些范例:

    • 对于接受 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 .

    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 .

    class PySide.QtGui. QValidator ( [ parent=None ] )
    参数: parent PySide.QtCore.QObject

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

    PySide.QtGui.QValidator. State

    此枚举类型定义可以存在经过验证的字符串状态。

    常量 描述
    QValidator.Invalid 字符串是 clearly 无效的。
    QValidator.Intermediate 字符串是合理的中间体值。
    QValidator.Acceptable 字符串是可接受的最终结果;即:它是有效的。
    PySide.QtGui.QValidator. fixup ( arg__1 )
    参数: 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.QtGui.QValidator. locale ( )
    返回类型: PySide.QtCore.QLocale

    返回用于验证器的区域设置。默认情况下,初始区域设置如同 QLocale()。

    另请参阅

    PySide.QtGui.QValidator.setLocale() QLocale.QLocale()

    PySide.QtGui.QValidator. setLocale ( locale )
    参数: 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()

    PySide.QtGui.QValidator. validate ( arg__1 , arg__2 )
    参数:
    • arg__1 – unicode
    • arg__2 PySide.QtCore.int
    返回类型:

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