
We bake cookies in your browser for a better experience. Using this site means that you consent.Read More
TheQValidator class provides validation of input text.More...
| Header: | #include <QValidator> |
| Inherits: | QObject |
| Inherited By: |
| enum | State { Invalid, Intermediate, Acceptable } |
| QValidator(QObject * parent = 0) | |
| ~QValidator() | |
| virtual void | fixup(QString & input) const |
| QLocale | locale() const |
| void | setLocale(const QLocale & locale) |
| virtual State | validate(QString & input, int & pos) const = 0 |
TheQValidator class provides validation of input text.
The class itself is abstract. Two subclasses,QIntValidator andQDoubleValidator, provide basic numeric-range checking, andQRegExpValidator provides general checking using a custom regular expression.
If the built-in validators aren't sufficient, you can subclassQValidator. The class has two virtual functions:validate() andfixup().
validate() must be implemented by every subclass. It returnsInvalid,Intermediate orAcceptable depending on whether its argument is valid (for the subclass's definition of valid).
These three states require some explanation. AnInvalid string isclearly invalid.Intermediate is less obvious: the concept of validity is difficult to apply when the string is incomplete (still being edited).QValidator definesIntermediate as the property of a string that is neither clearly invalid nor acceptable as a final result.Acceptable means that the string is acceptable as a final result. One might say that any string that is a plausible intermediate state during entry of anAcceptable string isIntermediate.
Here are some examples:
fixup() is provided for validators that can repair some user errors. The default implementation does nothing.QLineEdit, for example, will callfixup() if the user presses Enter (or Return) and the content is not currently valid. This allows thefixup() function the opportunity of performing some magic to make anInvalid stringAcceptable.
A validator has a locale, set withsetLocale(). It is typically used to parse localized data. For example,QIntValidator andQDoubleValidator use it to parse localized representations of integers and doubles.
QValidator is typically used withQLineEdit,QSpinBox andQComboBox.
See alsoQIntValidator,QDoubleValidator,QRegExpValidator, andLine Edits Example.
This enum type defines the states in which a validated string can exist.
| Constant | Value | Description |
|---|---|---|
QValidator::Invalid | 0 | The string isclearly invalid. |
QValidator::Intermediate | 1 | The string is a plausible intermediate value. |
QValidator::Acceptable | 2 | The string is acceptable as a final result; i.e. it is valid. |
Sets up the validator. Theparent parameter is passed on to theQObject constructor.
Destroys the validator, freeing any storage and other resources used.
[virtual]void QValidator::fixup(QString & input) constThis function attempts to changeinput 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.
Reimplementations of this function can changeinput 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.
Returns the locale for the validator. The locale is by default initialized to the same as QLocale().
See alsosetLocale() andQLocale::QLocale().
Sets thelocale that will be used for the validator. Unless setLocale has been called, the validator will use the default locale set withQLocale::setDefault(). If a default locale has not been set, it is the operating system's locale.
See alsolocale() andQLocale::setDefault().
[pure virtual]State QValidator::validate(QString & input,int & pos) constThis virtual function returnsInvalid ifinput is invalid according to this validator's rules,Intermediate 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), andAcceptable if the input is valid.
The function can change bothinput andpos (the cursor position) if required.
© 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.