QRegExpValidator Class

The QRegExpValidator class is used to check a string against a regular expression. 更多...

头: #include <QRegExpValidator>
qmake: QT += gui
实例化: RegExpValidator
继承: QValidator

特性

公共函数

QRegExpValidator (QObject * parent = Q_NULLPTR)
QRegExpValidator (const QRegExp & rx , QObject * parent = Q_NULLPTR)
~QRegExpValidator ()
const QRegExp & regExp () const
void setRegExp (const QRegExp & rx )

重实现公共函数

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

额外继承成员

详细描述

The QRegExpValidator class is used to check a string against a regular expression.

QRegExpValidator uses a regular expression (regexp) to determine whether an input string is Acceptable , 中间体 ,或 Invalid . The regexp can either be supplied when the QRegExpValidator is constructed, or at a later time.

QRegExpValidator determines whether a string is Acceptable or not, the regexp is treated as if it begins with the start of string assertion ( ^ ) and ends with the end of string assertion ( $ ); the match is against the entire input string, or from the given position if a start position greater than zero is given.

If a string is a prefix of an Acceptable string, it is considered 中间体 . For example, "" and "A" are 中间体 for the regexp [A-Z][0-9] (whereas "_" would be Invalid ).

For a brief introduction to Qt's regexp engine, see QRegExp .

用法范例:

// regexp: optional '-' followed by between 1 and 3 digits
QRegExp rx("-?\\d{1,3}");
QValidator *validator = new QRegExpValidator(rx, this);
QLineEdit *edit = new QLineEdit(this);
edit->setValidator(validator);
					

下文呈现一些验证器范例。在实践中,它们通常关联上文范例中的 Widget。

// integers 1 to 9999
QRegExp rx("[1-9]\\d{0,3}");
// the validator treats the regexp as "^[1-9]\\d{0,3}$"
QRegExpValidator v(rx, 0);
QString s;
int pos = 0;
s = "0";     v.validate(s, pos);    // returns Invalid
s = "12345"; v.validate(s, pos);    // returns Invalid
s = "1";     v.validate(s, pos);    // returns Acceptable
rx.setPattern("\\S+");            // one or more non-whitespace characters
v.setRegExp(rx);
s = "myfile.txt";  v.validate(s, pos); // Returns Acceptable
s = "my file.txt"; v.validate(s, pos); // Returns Invalid
// A, B or C followed by exactly five digits followed by W, X, Y or Z
rx.setPattern("[A-C]\\d{5}[W-Z]");
v.setRegExp(rx);
s = "a12345Z"; v.validate(s, pos);        // Returns Invalid
s = "A12345Z"; v.validate(s, pos);        // Returns Acceptable
s = "B12";     v.validate(s, pos);        // Returns Intermediate
// match most 'readme' files
rx.setPattern("read\\S?me(\.(txt|asc|1st))?");
rx.setCaseSensitive(false);
v.setRegExp(rx);
s = "readme";      v.validate(s, pos); // Returns Acceptable
s = "README.1ST";  v.validate(s, pos); // Returns Acceptable
s = "read me.txt"; v.validate(s, pos); // Returns Invalid
s = "readm";       v.validate(s, pos); // Returns Intermediate
					

另请参阅 QRegExp , QIntValidator , QDoubleValidator ,和 设置编辑器范例 .

特性文档编制

regExp : QRegExp

This property holds the regular expression used for validation

By default, this property contains a regular expression with the pattern .* that matches any string.

访问函数:

const QRegExp & regExp () const
void setRegExp (const QRegExp & rx )

成员函数文档编制

QRegExpValidator:: QRegExpValidator ( QObject * parent = Q_NULLPTR)

构造验证器采用 parent object that accepts any string (including an empty one) as valid.

QRegExpValidator:: QRegExpValidator (const QRegExp & rx , QObject * parent = Q_NULLPTR)

构造验证器采用 parent object that accepts all strings that match the regular expression rx .

The match is made against the entire string; e.g. if the regexp is [A-Fa-f0-9]+ it will be treated as ^[A-Fa-f0-9]+$ .

QRegExpValidator:: ~QRegExpValidator ()

销毁验证器。

[虚拟] QValidator::State QRegExpValidator:: validate ( QString & input , int & pos ) const

重实现自 QValidator::validate ().

返回 Acceptable if input is matched by the regular expression for this validator, 中间体 if it has matched partially (i.e. could be a valid match if additional valid characters are added), and Invalid if input is not matched.

Additionally, if input is not matched, the pos parameter is set to the length of the input 参数。

For example, if the regular expression is \w\d\d (word-character, digit, digit) then "A57" is Acceptable , "E5" is 中间体 , and "+9" is Invalid .

另请参阅 QRegExp::exactMatch ().