
We bake cookies in your browser for a better experience. Using this site means that you consent.Read More
TheQRegExpValidator class is used to check a string against a regular expression.More...
| Header: | #include <QRegExpValidator> |
| Instantiated By: | RegExpValidator |
| Inherits: | QValidator |
| QRegExpValidator(QObject * parent = 0) | |
| QRegExpValidator(const QRegExp & rx, QObject * parent = 0) | |
| ~QRegExpValidator() | |
| const QRegExp & | regExp() const |
| void | setRegExp(const QRegExp & rx) |
| virtual QValidator::State | validate(QString & input, int & pos) const |
TheQRegExpValidator class is used to check a string against a regular expression.
QRegExpValidator uses a regular expression (regexp) to determine whether an input string isAcceptable,Intermediate, orInvalid. The regexp can either be supplied when theQRegExpValidator is constructed, or at a later time.
WhenQRegExpValidator determines whether a string isAcceptable 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 anAcceptable string, it is consideredIntermediate. For example, "" and "A" areIntermediate for the regexp[A-Z][0-9] (whereas "_" would beInvalid).
For a brief introduction to Qt's regexp engine, seeQRegExp.
Example of use:
// regexp: optional '-' followed by between 1 and 3 digitsQRegExp rx("-?\\d{1,3}");QValidator*validator=newQRegExpValidator(rx,this);QLineEdit*edit=newQLineEdit(this);edit->setValidator(validator);
Below we present some examples of validators. In practice they would normally be associated with a widget as in the example above.
// integers 1 to 9999QRegExp 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 Invalids="12345"; v.validate(s, pos);// returns Invalids="1"; v.validate(s, pos);// returns Acceptablerx.setPattern("\\S+");// one or more non-whitespace charactersv.setRegExp(rx);s="myfile.txt"; v.validate(s, pos);// Returns Acceptables="my file.txt"; v.validate(s, pos);// Returns Invalid// A, B or C followed by exactly five digits followed by W, X, Y or Zrx.setPattern("[A-C]\\d{5}[W-Z]");v.setRegExp(rx);s="a12345Z"; v.validate(s, pos);// Returns Invalids="A12345Z"; v.validate(s, pos);// Returns Acceptables="B12"; v.validate(s, pos);// Returns Intermediate// match most 'readme' filesrx.setPattern("read\\S?me(\.(txt|asc|1st))?");rx.setCaseSensitive(false);v.setRegExp(rx);s="readme"; v.validate(s, pos);// Returns Acceptables="README.1ST"; v.validate(s, pos);// Returns Acceptables="read me.txt"; v.validate(s, pos);// Returns Invalids="readm"; v.validate(s, pos);// Returns Intermediate
See alsoQRegExp,QIntValidator,QDoubleValidator, andSettings Editor Example.
This property holds the regular expression used for validation.
By default, this property contains a regular expression with the pattern.* that matches any string.
Access functions:
| const QRegExp & | regExp() const |
| void | setRegExp(const QRegExp & rx) |
Constructs a validator with aparent object that accepts any string (including an empty one) as valid.
Constructs a validator with aparent object that accepts all strings that match the regular expressionrx.
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]+$.
Destroys the validator.
[virtual]QValidator::State QRegExpValidator::validate(QString & input,int & pos) constReimplemented fromQValidator::validate().
ReturnsAcceptable ifinput is matched by the regular expression for this validator,Intermediate if it has matched partially (i.e. could be a valid match if additional valid characters are added), andInvalid ifinput is not matched.
Thepos parameter is set to the length of theinput parameter.
For example, if the regular expression is\w\d\d (word-character, digit, digit) then "A57" isAcceptable, "E5" isIntermediate, and "+9" isInvalid.
See alsoQRegExp::exactMatch().
© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.