QValidator 类

QValidator class provides validation of input text. 更多...

头: #include <QValidator>
qmake: QT += gui
继承: QObject
继承者:

QDoubleValidator , QIntValidator , QRegExpValidator ,和 QRegularExpressionValidator

公共类型

enum State { Invalid, Intermediate, Acceptable }

公共函数

QValidator (QObject * parent = Q_NULLPTR)
~QValidator ()
virtual void fixup (QString & input ) const
QLocale locale () const
void setLocale (const QLocale & locale )
virtual State validate (QString & input , int & pos ) const = 0

信号

void changed ()

额外继承成员

详细描述

QValidator class provides validation of input text.

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

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

validate () 必须由每个子类来实现。它返回 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). QValidator 定义 中间体 作为字符串特性,作为最终结果这既不明显无效也不可接受。 Acceptable 意味着字符串可以作为最终结果被接受。有人可能会说,任何字符串都是合理中间状态,在输入期间 Acceptable 字符串是 中间体 .

这里是一些范例:

  • 对于接受 10 至 1000 (含 1000) 的整数的行编辑,42 和 123 是 Acceptable ,空字符串和 5 是 中间体 ,而 asdf 和 1114 是 Invalid .
  • 对于接受 URL 的可编辑组合框,任何良好格式的 URL 是 Acceptable ,http://example.com/, 是 中间体 (可能由于剪切和粘贴动作,意外在结尾加了个逗号),空字符串是 中间体 (用户可能选择并删除所有文本,预备键入新的 URL) 而 http:///./ 是 Invalid .
  • 对于接受长度的自旋框,11cm 和 1in 是 Acceptable ,11 和空字符串是 中间体 ,而 http://example.com 和 hour 是 Invalid .

fixup () 是为可以修理某些用户错误的验证器而提供的。默认实现什么都不做。 QLineEdit ,例如,会调用 fixup () 若用户按下 Enter 键 (或 Return 键) 且内容目前无效。这允许 fixup () 函数有机会履行一些魔法操作以使 Invalid string Acceptable .

验证器拥有区域设置,设置采用 setLocale ()。通常使用它来剖析本地化数据。例如, QIntValidator and QDoubleValidator 使用它来剖析整数和双精度数的本地化表示。

QValidator is typically used with QLineEdit , QSpinBox and QComboBox .

另请参阅 QIntValidator , QDoubleValidator , QRegExpValidator ,和 行编辑范例 .

成员类型文档编制

enum QValidator:: State

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

常量 描述
QValidator::Invalid 0 字符串是 clearly 无效的。
QValidator::Intermediate 1 字符串是合理的中间体值。
QValidator::Acceptable 2 字符串是可接受的最终结果;即:它是有效的。

成员函数文档编制

QValidator:: QValidator ( QObject * parent = Q_NULLPTR)

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

QValidator:: ~QValidator ()

销毁验证器,释放使用的任何存储和其它资源。

[signal] void QValidator:: changed ()

此信号被发射,当可能影响字符串有效性的任何特性改变时。

[virtual] void QValidator:: fixup ( QString & input ) const

此函数试图改变 input 为有效根据此验证器的规则。它不需要产生有效字符串:此函数的调用者之后必须重新测试;默认什么都不做。

此函数的重实现可以改变 input 即使没有产生有效字符串。例如,ISBN 验证器可能想要删除除数字和 - 外的每个字符,即使结果仍是无效 ISBN;姓氏验证器可能想要从字符串开头和结尾移除空格,即使结果字符串不在接受姓氏列表中。

QLocale QValidator:: locale () const

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

另请参阅 setLocale () 和 QLocale::QLocale ().

void QValidator:: setLocale (const QLocale & locale )

设置 locale 将用于验证器。除非已调用 setLocale,否则验证器将使用默认区域设置,设置采用 QLocale::setDefault ()。若默认区域设置尚未设置,则它是操作系统的区域设置。

另请参阅 locale () 和 QLocale::setDefault ().

[pure virtual] State QValidator:: validate ( QString & input , int & pos ) const

此虚函数返回 Invalid if input 是无效的根据此验证器规则, 中间体 若可能的话,稍微多做一些编辑将使输入可接受 (如:用户在接受 10 至 99 的整数的 Widget 中键入 4),及 Acceptable 若输入有效。

函数可以改变 input and pos (光标位置) 若有要求。